Objective-C没有其他语言内置的命名空间(namespace)机制。如果发生命名冲突程序连接时候,出现以下错误:
duplicate symbol _OBJC_METACLASS_$_EOCTheClass in:
build/something.o
build/something_else.o
duplicate symbol _OBJC_CLASS_$_EOCTheClass in:
build/something.o
build/something_else.o
错误的原因在于,应用程序中的两份代码都实现了EOCTheClass的类。避免此问题的唯一办法变相命名空间。
在使用Cocoa创建应用程序的时候一定要注意,Apple宣称其保留使用所有“两个字母前缀”,所以你自己选用的前缀应该是三个或以上。
NSLog(@"object =%@",object);
● +string//用于创建新的空字符串
● +stringWithString//工厂方法,用于根据某字符串创建新的字符串
● +localizedStringWithFormat://工厂方法,格式化本地字符串
● -lowercaseString//把字符串中的大写字母全部转化位小写
● -intValue//将字符串解析为整数
● -length//获取字符串长度
● -lengthOfBytesUsingEncoding://若字符串是以给定的编码格式
● -getCharacters:range://获取字符串给定范围的字符
● -(void)getCharacters:(unichar*)buffer range:(NSRange)aRange//首个参数应该指向一个足够大的数组,以便容下请求范围内的那些字符串
● -hasPrefix://是否有某个前缀
● -isEqualToString://判断字符串是否相等
-(id)copyWithZone:(NSZone*)zone
#import
@interface EOCPerson :NSObject
@propetry (nonatomic,copy,readonly) NSString *firstName;
@propetry (nonatomic,copy,readonly) NSString *lastName;
-(id) initWithFirstName:(NSString*)firstName andLastName:(NSString*)lastName;
-(void)addFriend:(EOCPerson*) person;
-(void)removeFriend:(EOCPerson*)person;
@end
@implementation EOCPerson{
NSMutableSet *_friends;
}
-(id) initWithFirstName:(NSString*)firstName andLastName:(NSString*)lastName{
if((self = [super init])){
_firstName = [firstName copy];
_lastName = [lastName copy];
_friends = [NSMutableSet new];
}
return self
}
-(void)addFriend:(EOCPerson*)person{
[_friends addObject:person];
}
-(void) removeFriend:(EOCPerson*)person{
[_friends removeObject:person];
}
-(id)copyWithZone:(NSZone)zone{
EOCPerson *copy =[[[self class]allocWithZone:zone] initWithFirstName:_firstName andLastName:_lastName];
copy->_friends = [_friends mutableCopy];
return copy
}
@end
-(id)initWithSet:(NSArray*)array copyItems:(BOOL)copyItems
// 非容器类对象
NSString *str = @"origin string";
NSString *strCopy = [str copy];
NSMutableString *mstrCopy = [str mutableCopy];
[mstrCopy appendString:@"??"];
NSMutableString *mstr = [NSMutableString stringWithString:@"origin"];
NSString *strCopy = [mstr copy];
NSMutableString *mstrCopy = [mstr copy];
NSMutableString *mstrMCopy = [mstr mutableCopy];
//[mstrCopy appendString:@"1111"]; //error
[mstr appendString:@"222"];
[mstrMCopy appendString:@"333"];
【本章要点】