CS193p Lecture 4 - Foundation, Attributed Strings

消息机制

  调用一个实例(instance)的方法(method),就是向该实例的指针发送消息(message),实例收到消息后,从自身的实现(implementation)中寻找响应这条消息的方法。

 

id

  id myObject;

声明了一个指针,类型是id,id表示它是一个指针(不用id *)指向一个我们不知道类型的对象。

 

异常和未知选择器(P60)

  OC的对象都有一个名为 isa 的实例变量,指向创建该对象的类。

  对象职能响应类中具有的相应方法的信息。但是xcode在编译时无法判断某个对象是否能响应特定的信息,对象的类型只能在运行时确定。

  如果向某个对象发送其无法响应的消息,只会有warning,编译仍然可以通过,但是运行时就会抛出异常(exception)。

 

1、id

2、id <UIScrollViewDelegate> scrollViewDelegate

3、NSSting

第1类,id,我们对它指向的类型毫无所知;

第3类,比如NSSting,我们完全知晓;

而第2类,id加上一个协议(尖括号框起来),表示我们虽然对它指向的类型不知,但是它必须实现协议中制定的方法;

 

introspection(内省)

isKindOfClass: 对象是否属于某一类(包含类及其子类)

  [obj isKindOfClass:[你要考虑的类 class]]

isMemberOfClass: 对象是否是某一类(仅类本身,不包含子类)

respondsToSelector: 对象能否对某一方法做出反应

  [obj respondsToSelector:@selector(shootAt:)]

 

 

- (id)copy  

- (id)mutableCopy

NSArray、NSDictionary等实现了copy、mutableCopy(NSObject并未实现)

将copy无论发送给什么对象,返回的始终是不可变对象;

将mutableCopy无论发送给什么对象,返回的始终是可变对象;

 

NSArray(重要)

一经创建就无法改变,无法增加元素,也无法删除元素;

最常见的创建方法:@[元素A, 元素B, 元素C...]

count、objectAtIndex:、lastObject、firstObject

 

NSMutableArray

+ (instancetype)arrayWithCapacity:(NSUInteger)numItems;

+ (instancetype)array;  // [NSMutableArray array] 等同于 [[NSMutableArray alloc] init]

- (void)addObject:(id)anObject;

- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;

- (void)removeObjectAtIndex:(NSUInteger)index;

可以用下标来简便表示

 

NSNumber

初始化方法

+ (NSNumber *)numberWithInt:(int)value;

NSNumber *three = @3;

NSNumber *underline = @(NSUnderlineStyleSingle);  // 枚举 enum

 

NSData

二进制数据包

 

NSDate

相关的:NSCalendar, NSDateFormatter, NSDateComponents

 

NSSet

其中的元素是hash存储的,查找起来比NSArray高;

 

NSDictionary(重要)

初始化方法(NSString是最常用的key)

NSDictionary *colors = @{ @"green": [UIColor greenColor],

                          @"blue":  [UIColor blueColor],

                          @"red":   [UIColor redColor]

                          };

- (id)objectForKey:(id)aKey;

color["blue"]

NSMutableDictionary

- (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey;

 

Property List

NSUserDefaults

在真实的App开发中,我们常常将这些静态元素存放在外部(如菜谱列表)文件或数据库或其它地方。在IOS编程中,有一种类型的文件,成为 Property List. 这一类型的文件通常在Mac OS 和 iOS中发现,用来存放简单的结构数据(如应用程序设置

我觉得可以理解为配置文件,通常是轻量级的,但是又不宜写在代码中的;

http://alan-hjkl.iteye.com/blog/1678505

 

NSRange

typedef struct{

    NSUInteger location;

    NSUInteger length;

} _NSRange;

typedef NSRange *NSRangePointer;

iOS中不会将struct放到堆中,这个 NSRangePointer 用于引用调用 range

 

UIColor

UIColor *color = [UIColor greenColor];

newColor = [color colorWithAlphaComponent:0.3];

// alpha 表示透明度,1表示不透明,0表示全透明

 

UIFont

系统推荐字体(建议用于正文)

UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];

"system" fonts(建议用于按钮等)

+ (UIFont *)systemFontOfSize:(CGFloat)fontSize;

+ (UIFont *)boldSystemFontOfSize:(CGFloat)fontSize;

UIFontDescriptor

 

NSAttributedString

- (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range;

http://www.cnblogs.com/whyandinside/archive/2013/12/27/3493475.html

NSAttributedString管理一个字符串,以及与该字符串中的单个字符或某些范围的字符串相关的属性。比如这个字符串“我北京天安门”,“我”跟其他字符的颜色不一样,而“北京”与其他的字体和大小不一样,等等。NSAttributedString就是用来存储这些信息的,具体实现时,NSAttributedString维护了一个NSString,用来保存最原始的字符串,另有一个NSDictionary用来保存各个子串/字符的属性。

NSAttributedString * attStr = ...

NSString *str = [attStr string];

如上所说,NSAttributedString维护了一个NSString,可以用string的读方法获取到这个原始字符串;

 

UIButton

- (void)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)state;

 

UILabel

@property(nonatomic,copy)   NSAttributedString *attributedText;

 

posted @ 2015-03-29 22:03  mobilefeng  阅读(224)  评论(0编辑  收藏  举报