美文网首页
Objective-C tips

Objective-C tips

作者: 哆啦A梦频道 | 来源:发表于2017-01-02 16:02 被阅读0次

1、初始化NSAttributedString时要校验string是否为空,为空会导致闪退。

NSAttributedString *str = [[NSAttributedString alloc] initWithString:nil attributes:@{NSForegroundColorAttributeName:[UIColor redColor]}];

2、下面的代码是错误的,当someString有值时是正确的,当等于nil时,会出现的错误的结果。

NSString *someString = nil;
NSRange range = [someString rangeOfString:@"hello"];
NSLog(@"[someString=%@,range=%@]", someString, NSStringFromRange(range));
if (range.location != NSNotFound) {
    NSLog(@"find hello!");
}

someString = nil时会得到下面的输出:

[someString=(null),range={0, 0}]
find hello!

3、判断NSValue的具体的数据类型

NSValue *value = [NSValue valueWithCGPoint:CGPointMake(10, 10)];
BOOL isCGPoint = strcmp(value.objCType, @encode(CGPoint)) == 0;

同样的,

BOOL isCGSize = strcmp(value.objCType, @encode(CGSize)) == 0;
BOOL isCGRect = strcmp(value.objCType, @encode(CGRect)) == 0;
BOOL isUIEdgeInsets = strcmp(value.objCType, @encode(UIEdgeInsets)) == 0;
BOOL isUIOffset = strcmp(value.objCType, @encode(UIOffset)) == 0;
BOOL isCGAffineTransform = strcmp(value.objCType, @encode(CGAffineTransform)) == 0;
BOOL isCGVector = strcmp(value.objCType, @encode(CGVector)) == 0;

相关文章

  • Effective Objective-C 2.0 Tips 总

    Effective Objective-C 2.0 Tips 总结 Chapter 5,6,7 Chapter 5...

  • Swift单例模式

    参考:http://swifter.tips/singleton/ OC写法 在 Objective-C 中单例的...

  • 编译标记

    转载至:http://swifter.tips/param-mark/ 在 Objective-C 中,我们经常在...

  • Objective-C tips

    1、初始化NSAttributedString时要校验string是否为空,为空会导致闪退。 2、下面的代码是错误...

  • Objective-C Tips

    1、oc中各种nil。 nil用来表示空对象,数组、字典结束判断 Nil用于类的空指针 NSNull用来标识什么都...

  • Effective Objective-C 2.0 Tips 总

    Chapter 3 接口与 API 设计 Tips 15 使用前缀避免明明空间冲突Objective-C 没有命名...

  • Objective-C 编码 Tips

    [toc] 一般性原则 避免命名歧义 Not Preferred 一致性 整个工程的命名风格要保持一致性,最好和A...

  • 《禅与Objective-C 编程艺术》读书笔记

    前言 《禅与Objective-C 编程艺术》虽然内容不是很多,但是却又很多好用的Tips。可以将其作为OC的编码...

  • iOS Develop Tips

    前言 记录一些代码小技巧持续更新?! Objective-C tips 1、使控件从导航栏以下开始 2、将navi...

  • js 兼容 hack

    tips tips tips tips tips

网友评论

      本文标题:Objective-C tips

      本文链接:https://www.haomeiwen.com/subject/wzvwvttx.html