新手帖1,随意吐槽!
上代码:
.h
/*自定义对象 用于获取通讯录信息*/
@interface AddressBook : NSObject {
NSString *firstname;
NSString *lastname;
NSString *compositename;
NSString *tel;
}
@property (nonatomic, retain) NSString *firstname;
@property (nonatomic, retain) NSString *lastname;
@property (nonatomic, retain) NSString *compositename;
@property (nonatomic, retain) NSString *tel;
@end
.m
/*可变对象数组 用于存放通讯录信息*/
@property (copy,nonatomic) NSMutableArray *addressBookTemp;
@implementation AddressBook
@synthesize tel,firstname,lastname,compositename;
@end
/*解析vcf格式通讯录,获取firstname lastname compositename tel并存放到对象数组addressBookTemp*/
-(void)Parasecontent:(NSString*)vcardString{
self.addressBookTemp = [NSMutableArray array];
AddressBook addressBook;
NSLog(@"llllll %@",vcardString);
NSArray *lines = [vcardString componentsSeparatedByString:@"\n"];
for(NSString* line in lines)
{
if ([line hasPrefix:@"BEGIN:"])
{
addressBook = [[AddressBook alloc] init];/*报错1 copyWithZone:]: unrecognized selector sent to instance*/
NSLog(@"parse start");
}
else if([line hasPrefix:@"END"])
{
[self.addressBookTemp addObject:addressBook];/*报错2 addObject:]: unrecognized selector sent to instance*/
NSLog(@"parse end");
}
else if([line hasPrefix:@"VERSION:"]){
continue;
}
else if([line hasPrefix:@"FN:"])
{
NSArray *upperComponents = [line componentsSeparatedByString:@":"];
if([upperComponents count]>1){
NSString * personName = [upperComponents objectAtIndex:1];
addressBook.compositename = personName;
NSLog(@"personName %@ ",personName);
}
}
else if([line hasPrefix:@"N:"])
{
NSArray *component = [line componentsSeparatedByString:@":"];
if([component count]>1){
NSArray *components = [component[1] componentsSeparatedByString:@";"];
NSString *firstname = [components objectAtIndex:0];
addressBook.firstname = firstname;
if([components count]>1){
NSString *lastname = [components objectAtIndex:1];
self.addressBook1.lastname = lastname;
}
}
}
else if([line hasPrefix:@"TEL;"])
{
NSArray *components = [line componentsSeparatedByString:@":"];
if([components count]>1){
NSString *personPhone = [components objectAtIndex:1];
addressBook.tel = personPhone;
}
}
}
}
先看报错1:
Foundation类已经遵守了<NSCopying>和 <NSMutableCopying>协议,即实现了copy和mutableCopy方法,因此Foundation对象可以使用这些方法创建对象的副本或可变副本。
但在此例中,并不是Foundation对象,而是自定义的对象!!!
So,用户自定义类遵守<NSCopying>协议和<NSMutableCopying>协议,则必须实现copyWithZone方法和mutableCopyWithZone方法,否则该类对象无法响应copy和mutableCopy消息。
参考文章:
http://www.2cto.com/kf/201312/267583.html
那么问题来了,在此例中需要实现copyWithZone和mutableCopyWithZone这两种方法吗?当然报错信息很明显提示是要实现copyWithZone方法,那mutableCopyWithZone方法需要吗?继续深究,什么情况下实现copyWithZone和mutableCopyWithZone方法?
参考文章:
http://www.2cto.com/kf/201110/107586.html
http://my.oschina.net/xrk/blog/198888?p=1
解决报错1:实现copyWithZone方法(绿色为修改添加部分)
.m
/*可变对象数组 用于存放通讯录信息*/
@property (copy,nonatomic) NSMutableArray *addressBookTemp;
@implementation AddressBook
@synthesize tel,firstname,lastname,compositename;
- (id)init
{
if (self = [super init])
{
self.firstname = [[NSString alloc]init];
self.lastname = [[NSString alloc]init];
self.compositename = [[NSString alloc]init];
self.tel = [[NSString alloc]init];
}
return self;
}
- (id)copyWithZone:(NSZone *)zone
{
AddressBook *copy = [[[self class] allocWithZone:zone] init];
copy->firstname = [firstname copy];
copy->lastname = [lastname copy];
copy->compositename = [compositename copy];
copy->tel = [tel copy];
return copy;
}
@end
报错2:
代码中对象数组初始化: self.addressBookTemp = [NSMutableArray array];
应该改为[[NSMutableArray array]retain] 或 [[NSMutableArray alloc]init];
且@property (copy,nonatomic) NSMutableArray *addressBookTemp;
改为@property (retain,nonatomic) NSMutableArray *addressBookTemp;
参考文章:
http://blog.csdn.net/chiuan/article/details/8274295
http://www.apkbus.com/android-701-1.html