ios 一些知识


-(void) report_memory { struct task_basic_info info; mach_msg_type_number_t size =sizeof(info); kern_return_t kerr = task_info(mach_task_self(), TASK_BASIC_INFO,(task_info_t)&info,&size); if( kerr == KERN_SUCCESS ) {NSLog(@"Memory in use (in bytes): %u", info.resident_size);} else{NSLog(@"Error with task_info(): %s", mach_error_string(kerr)); } }

显示当前内存使用

启动时间优化:

ios app launch time optimization

https://developer.apple.com/videos/wwdc/2012/

 

将oc 文件翻译成 c 文件

clang -rewrite-objc *m

clang 的相关参数的可以从xcode里的编译命令里取,

如 带上arc

 clang -fobjc-arc  -rewrite-objc main.m -o main2.cpp

 

os 输出预处理后的文件

On the command line, gcc -E foo.m will show you the preprocessed output (just as it does for normal C/C++ files). Of course, this will also expand any #include or #import statements you may have in your code.

 

From within Xcode:

  • Xcode 3: Select the file, then Build → Preprocess.
  • Xcode 4: Select the file, then Product → Generate Output → Generate Preprocessed File.
  • Xcode 5: Product → perform action → Preprocessed File.

 

 

打印当前的ui树

[UIWindow keyWindow] recursiveDescription]

只能打印最顶层的窗口,可以找下打印整个ui树的(枚举所有的窗口,让后调用recursiveDescription)

_UIModalItemHostingWindow 弹出uialertview时 顶层的窗口是这个

参考 http://stackoverflow.com/questions/5150186/how-do-i-inspect-the-view-hierarchy-in-ios

 

字体的创建耗时:

CTFontRef font = CTFontCreateWithName((CFStringRef)@"STHeitiSC-Light", fontSize, NULL);
CGFontRef cgFont = CGFontCreateWithFontName((CFStringRef)@"STHeitiSC-Light");

第一次创建的时候会耗时,再次创建的时候就不会了。

iOS开发 .framework的Optional(弱引用)和Required(强引用)区别

 首先,参考文档:https://blog.stackmob.com/2013/03/objective-c-tip-of-the-month-optional-frameworks/

强引用(Required)的framework是一定会被加载到内存的,但是弱引用(Optional)的framework只在需要时才会被载入内存,这对于比较大的framework来说,在最初加载的时候会省很多时间。

简单解释一下,有一些库如Social.framework 和 AdSupport.framework,是在iOS6之后才被引入的,还有一些更新了新特性的只能在iOS6+上可用。当你添加一个framework到你的工程里,他们被默认强引用(Required),然而,当你最终把程序配置在运行5.0的设备上时,你会发现它通不过最户的加载,原因就在于这些库是不被iOS5.0支持的,就需要我们把这些库的引用改为Optional.

其次,如果你遇见了这个错误:duld:Library not found………………说明你有不应该强引用的可存在,这个错误报告里都会指明有哪些库需要弱引用。

 

ios判断系统的版本

From some example in Apple Documentation :

if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
    // < = 6.1
} else {
    // > = 7
}
2、[[UIDevice currentDevice] systemVersion]

第一种会好点。

 

 

这个是同步

[self performSelector];

 这个是异步

  [self performSelector:@selector() onThread:[NSThread currentThread] withObject:nil waitUntilDone:NO];

 

增加nstimer 的模式

[runloop addTimer:timer forMode:NSRunLoopCommonModes];
[runloop addTimer:timer forMode:UITrackingRunLoopMode];

 

判断程序因为啥被杀:

http://stackoverflow.com/questions/12718341/ios-6-saving-restoring-app-state-feature 

http://stackoverflow.com/questions/15525513/how-can-we-check-the-ios-app-killed-by-user-or-ios

shouldRestoreApplicationState: 如果是非安全的杀掉(用户主动杀的属于安全的杀掉),那么在重新启动的时候会调用这个

 

获取cpu 的个数

method1:

#include <mach/mach_host.h>

int countCores()
{
  host_basic_info_data_t hostInfo;
  mach_msg_type_number_t infoCount;

  infoCount = HOST_BASIC_INFO_COUNT;
  host_info( mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo, &infoCount ) ;

  return hostInfo.max_cpus ;
}

Method 2

#include <sys/sysctl.h>

unsigned int countCores()
{
  size_t len;
  unsigned int ncpu;

  len = sizeof(ncpu);
  sysctlbyname ("hw.ncpu",&ncpu,&len,NULL,0);

  return ncpu;
}

 

 

NSOperationQueue 中得cancelAllOperations   对已经在运行的任务不能取消。

    [drawOperationQueue addOperationWithBlock: ^{
        MyCustomClass * test = [[MyCustomClass alloc] init];
        if(test)
        {
           NSLog(@"in operation");
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [drawOperationQueue cancelAllOperations];
            NSLog(@"queue canceldone");
            
        });
        
        for(int i =0;i<100;++i)
        {
            NSLog(@"for%d",i );
        }
        
    }];

 上面的代码中,cancelAllOperations 已经执行完成,但下面的for循环还是一直都在执行中。

 

initialize 会执行多次

+initialize will be sent to each class the first time it is referenced (by message), including dynamically created classes. There is no protection in the runtime against triggering execution multiple times. If a subclass is initialized, but doesn't implement +initialize, whatever super up the chain will have theirs called again.

Orthogonally, automatic KVO is implemented by creating dynamically derived subclasses of the class of the observed instance. That subclass is +initialized just like any other class, thus triggering multiple executions of the parent class's +initialize.

The runtime could take measures to protect against this. However, since +initialize has always been documented as potentially being executed multiple times, that added complexity (it is surprisingly complex, given that KVO classes come and go quite frequently, potentially) isn't deemed worth the effort.

The current recommended pattern is:

+ (void) initialize
{
      static dispatch_once_t once;
      dispatch_once(&once, ^{
        ... one time initialization here ...
      });
}

圆形按钮
-(void)roundButtonDidTap:(UIButton*)tappedButton;

.m

#define ROUND_BUTTON_WIDTH_HEIGHT YourButtonWidthToBeSetHere

-(void)roundButtonDidTap:(UIButton*)tappedButton{

    NSLog(@"roundButtonDidTap Method Called");

}



UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

[button setImage:[UIImage imageNamed:@"TimoonPumba.png"] forState:UIControlStateNormal];

[button addTarget:self action:@selector(roundButtonDidTap:) forControlEvents:UIControlEventTouchUpInside];

//width and height should be same value
button.frame = CGRectMake(0, 0, ROUND_BUTTON_WIDTH_HEIGHT, ROUND_BUTTON_WIDTH_HEIGHT);

//Clip/Clear the other pieces whichever outside the rounded corner
button.clipsToBounds = YES;

//half of the width
button.layer.cornerRadius = ROUND_BUTTON_WIDTH_HEIGHT/2.0f;
button.layer.borderColor=[UIColor redColor].CGColor;
button.layer.borderWidth=2.0f;


如果在arc 实现init 那么在后面return self,这个操作会导致对self retain 一次, 同时将它加到releasepool里。 即多了个加1和减1的操作。


手机重启的可能性

Completely exhausting the system memory triggers a hard reboot of the device. This used to be more common in iPhone OS 2.0, running on the limited hardware of the initial iPhones and iPod touches. In recent OS versions, Apple has more rigidly enforced the hard kill of your application when it exceeds its memory ceiling, so it's become much harder to do this. Also, the devices have much more memory than they used to.

One way that you can sometimes do this is by loading many large textures or other graphical components that may not be immediately identified as memory used by your application. I've been able to cause a system reboot when loading a pile of data onto the GPU in a tight loop. You may be encountering something similar here.

I doubt that this is related to the number of active threads you have going, although they probably make it easier for you to dump a mess of data into memory before the system can kill your application.

As an aside, rather than having piles of threads, which consume resources, have you looked at using GCD or a queue-based framework like ASIHTTPRequest? These might be more efficient for your application, yet still provide the concurrency you need.

 

在c++代码里打断点

info functions 函数名

先用此命令找到完整函数名称,和函数对应的地址

break 函数名或*函数地址

再用这条命令在系统函数中打断点

delete breakpoints 1 删除断点

info 是gdb的命令,现在xcode用得lldb,lldb和gdb命令的转换,可以参考 http://lldb.llvm.org/lldb-gdb.html

 

image lookup -r -s WebCore::WebVideoFullscreenModelMediaElement::updateForEventName

breakpoint set --name
breakpoint set --name WebCore::WebVideoFullscreenModelMediaElement::updateForEventName

po $r0  在汇编里打印当前类信息

 

打印ui控件树

po [[UIWindow keyWindow] recursiveDescription]

 

ios 默认编译的是thumb-2指令,也就是大部分指令只占两个字节,有些占四个字节

arm64编译是一个指令占四个字节(好像没有指令占8个字节),一个指针是占8个字节。

 

posted on 2014-05-23 10:13  tanglaoya321  阅读(324)  评论(0)    收藏  举报