Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在对象内使用as NSString进行NSCoding_Iphone_Objective C_Nsuserdefaults_Nskeyedarchiver_Nscoding - Fatal编程技术网

Iphone 在对象内使用as NSString进行NSCoding

Iphone 在对象内使用as NSString进行NSCoding,iphone,objective-c,nsuserdefaults,nskeyedarchiver,nscoding,Iphone,Objective C,Nsuserdefaults,Nskeyedarchiver,Nscoding,我的问题是,当我检索存储对象的NSArray时,我的所有NSString属性都会导致BadAccess错误。int和double属性工作正常 商店 @interface Store : NSObject<NSCoding> { NSString *Name; NSString *Address; NSString *Phone; double GeoLong; double GeoLat; int ID; } @pr

我的问题是,当我检索
存储
对象的NSArray时,我的所有NSString属性都会导致
BadAccess
错误。int和double属性工作正常

商店

@interface Store : NSObject<NSCoding> {
    NSString *Name;
    NSString *Address;
    NSString *Phone;
    double GeoLong;
    double GeoLat;
    int ID;         
}

@property (nonatomic, retain) NSString *Name;
@property (nonatomic, retain) NSString *Address;
@property (nonatomic, retain) NSString *Phone;
@property (nonatomic) double GeoLat;
@property (nonatomic) double GeoLong;
@property (nonatomic) int ID;

@end
这是我存储和检索数组的代码

//streams contains the data i will populate my array with. 
for (ndx = 0; ndx < streams.count; ndx++) {
            NSDictionary *stream = (NSDictionary *)[streams objectAtIndex:ndx];

            Store *item = [[Store alloc] init] ;
            item.Name = [stream valueForKey:@"Name"];
            item.Address = [stream valueForKey:@"Address"];
            item.Phone = [stream valueForKey:@"Phone"];
            item.GeoLat = [[stream valueForKey:@"GeoLat"] doubleValue];
            item.GeoLong = [[stream valueForKey:@"GeoLong"] doubleValue];                
            item.ID = [[stream valueForKey:@"ID"] intValue]; 

            [listToReturn addObject:item];
        }
    }

    //test to check if it works
    for(int i = 0; i < [listToReturn count]; i++){
        Store *item = (Store *)[listToReturn objectAtIndex:i];
        NSLog(@"Name: %@", item.Name); //works
    }

    //save
    [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:listToReturn] forKey:@"stores"];

    // retrieve
    NSMutableArray *stores = [NSMutableArray new];
    NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
    NSData *dataRepresentingSavedArray = [currentDefaults objectForKey:@"stores"];
    if (dataRepresentingSavedArray != nil)
    {
        NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
        if (oldSavedArray != nil)
            stores = [[NSMutableArray alloc] initWithArray:oldSavedArray];
        else
            stores = [[NSMutableArray alloc] init];
    }

    if ([stores count] > 0) {
        NSMutableArray * annotations =  [[NSMutableArray alloc] init];
        for(int i = 0;i< [stores count]; i++){

            Store *store = [stores objectAtIndex: i];

            CLLocationCoordinate2D location;
            if(store.GeoLat != 0 && store.GeoLong != 0){
                location.latitude = store.GeoLat;
                location.longitude = store.GeoLong; //works 
                NSLog(@"Adding store: %@", store.Name); //DONT WORK!! <-- MAIN PROBLEM
            }
        }
    }
//流包含我将填充数组的数据。
对于(ndx=0;ndx0){
NSMutableArray*注释=[[NSMutableArray alloc]init];
对于(int i=0;i<[存储计数];i++){
Store*Store=[stores objectAtIndex:i];
CLLOCATION坐标2D定位;
if(store.GeoLat!=0&&store.GeoLong!=0){
location.latitude=store.GeoLat;
location.longitude=store.GeoLong;//有效

NSLog(@“Adding store:%@”,store.Name);//不工作!!您没有在
initWithCoder
中保留属性

Name = [decoder decodeObjectForKey:@"Name"];
没有使用您定义的(保留)属性的setter。您只是设置ivar。这意味着您无法获得所有权,并且可以将其解除分配

以下是在您的案例中保留属性的两种方法:

self.Name = [decoder decodeObjectForKey:@"Name"];
Name = [[decoder decodeObjectForKey:@"Name"] retain];

令人惊讶,岩石,你也是:)好的和丰富的解释:)正是我想要的:)嘿,我意识到这与手头的问题无关,仅供参考。用大写字母开头变量名违反了所有的风格准则。在objective-c中,你的IVAR应该在第一个字母上有小的大写字母,类应该有u第一个字母的大写字母…只是一个惯例。
self.Name = [decoder decodeObjectForKey:@"Name"];
Name = [[decoder decodeObjectForKey:@"Name"] retain];