IOS开发的一些技巧以及笔记

1、在arc工程中添加不支持arc的类库时,编译错误
    解决方法:在编译报错的文件中添加-fno-objc-arc标识,使工程得以顺利编译。
       添加方法,工程文件——>Build Phases ——>Compile Sources——>报错文件——>双击报错文件,添加-fno-objc-arc标识。
2、delegate使用
     (1)声明delegate的方法
     (2)设置在什么情况下调用这个方法
     (3)在使用的类的.h文件中包含<XXXdelegate>
     (4)实现delegate的方法
     (5)设置XXXdelegate.delegate = 实现delegate方法的类
 
3、调用别人封装好的控件:
    ①、看别人的demo怎么调用该控件

    ②、看看该控件的接口属性


4、如何快速打印Frame

    ①一般会使用:NSLog(@"Frame is %g, %g, %g, %g", frame.origin.x, frame.origin.y, frame.size.width, frame.size.height); 

    ②方便的办法:NSLog(@"Frame is %@", NSStringFromCGRect(frame));


5、在UI设计的时候应把viewController和view分开,各个view之间、view与viewcontroller之间的交互使用delegate,但是页面的相互切换需要使用导航等切换方式。注:交互和切换是不同的概念


6、delegate的函数放到哪里都没有关系,只要能够调到就行,要是多个页面要实现这个delegate,就得把这个delegate的函数调出去,作为单独的文件。

7、使用NSFileManager管理文件
     ①、 // 取得 document 目录的路径列表
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

   ②、//document 目录的路径列表中的第一个目录就是 document 目录的路径
    NSString *documentsDirectory = [paths objectAtIndex:0];
   ③、 // 创建文件管理对象     NSFileManager *fm = [NSFileManagerdefaultManager];

   ④、 // 转到 document 的当前目录     [fm changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];

   ⑤、 // 在当前目录下创建 testFile.txt 文件     if ([fm createFileAtPath:@"testFile1.txt"contents:nilattributes:nil] == NO) {        NSLog(@"create file error >>>>>>>>>");
      }
   ⑥、// 获取 testFile.txt 文件所在的路径
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"testFile.txt"];
   ⑦、// 待写入的数据

      NSString *temp = @"Hello friend";    

      int data0 = 100000;    

      float data1 = 23.45f;        

   ⑧、//创建数据缓冲    

      NSMutableData *writer = [[NSMutableDataalloc] init];        

   ⑨、//将字符串添加到缓冲中    

      [writer appendData:[temp dataUsingEncoding:NSUTF8StringEncoding]];        

   ⑩、//将其他数据添加到缓冲中    

      [writer appendBytes:&data0 length:sizeof(data0)];    

      [writer appendBytes:&data1 length:sizeof(data1)];            

  (11)//将缓冲的数据写入到文件中    

      [writer writeToFile:path atomically:YES];        

  (12)//删除文件    

       if ([fm removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:@"testFile1.txt"] error:nil] ==         NO) {        

              NSLog(@"remove file error >>>>>>>>>");    

            }


8、完美破解Xcode
http://blog.sina.com.cn/s/blog_6ec3c9ce010196ad.html

注意事项:
(1)在输入,命令设置权限的时候要注意,先转到su下面,输入密码,再输入: chmod 777 gen_entitlements.py
(2) 这段话不能少(在每一个项目中都要添加):
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1.选中项目后,点击Build Phases,在右下方看见Add Build Phase
2.选择Add Build Phase ,然后选择Run Script ,再在shell下的输入框中添加一下内容:
export CODESIGN_ALLOCATE=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/codesign_allocate

if [ "${PLATFORM_NAME}" == "iphoneos" ] || [ "${PLATFORM_NAME}" == "ipados" ]; then

/Applications/Xcode.app/Contents/Developer/iphoneentitlements/gen_entitlements.py "my.company.${PROJECT_NAME}" "${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/${PROJECT_NAME}.xcent";

codesign -f -s "iPhone Developer" --entitlements "${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/${PROJECT_NAME}.xcent" "${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/"
 
fi

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>



9、未使用的成员变量在XCode4.6下的编译错误

XCode4.6加强了对编译的检查,如果一个类中有未使用的私有成员变量,在XCode4.6中会报编译错误。

如果是自己的代码,那么应该立即更改。但是如果是第三方的库代码,自己更改不利于以后升级代码,所以最好加上如下的编译参数:
- Wno -unused- private -field

我的一个iOS项目使用了ZXing库来扫描二维码,正好Zxing库就有上述问题,在项目的 Build Settings中搜索 Other Warning Flags项,然后将这项的值增加:-Wno-unused-private-field即可。如下所示:



10、从Xcode4.5之后,可是像定义字符串那样来快速定义NSNumber、NSArray和NSDictionary

①、定义一个NSString对象:NSString *aString = @"This is a test string";

②、定义一个NSNumber对象:

NSNumber *n1 = @1000;  // [NSNumber numberWithInt:1000] 
NSNumber *n2 = @3.1415926; // [NSNumber numberWithDouble:3.1415926]

NSNumber *anotherInteger = @42; //NSNumber *anInteger = [NSNumber numberWithInt:42];

③、定义一个NSArray对象:

// before
NSArray *words = [NSArray arrayWithObjects:@"array", @"of", @"words", nil];
// after (array with some strings and numbers)

NSArray *words = @[@"array", @"of", @"words", @1, @1.4];

④、定义一个NSDictionary对象:

//Old way
NSDictionary *oldDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@1, @"key1", @2, @"key2"];
//New way to do it
NSDictionary *newDictionary = @{ @”key1” : @1,  @”key2”: @2 };


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值