iphone - NSCoding with as NSString inside a object

标签 iphone objective-c nsuserdefaults nskeyedarchiver nscoding

我的问题是我检索了 Store 对象的 NSArray,我的所有 NSString 属性都导致了 BadAccess 错误。 int 和 double 属性工作正常!

商店.h

@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

商店.m

@implementation Store

@synthesize Name;
@synthesize ID;
@synthesize Address;
@synthesize Phone;
@synthesize GeoLat;
@synthesize GeoLong;


/** Implentation of the NSCoding protocol. */

-(void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeInt:ID forKey:@"ID"];
    [encoder encodeDouble:GeoLat forKey:@"GeoLat"];
    [encoder encodeDouble:GeoLong forKey:@"GeoLong"];
    NSLog(@"Name in encode: %@", Name); //WORKS!
    [encoder encodeObject:Name forKey:@"Name"];
    [encoder encodeObject:Phone forKey:@"Phone"];
    [encoder encodeObject:Address forKey:@"Address"];

}

-(id)initWithCoder:(NSCoder *)decoder
{
    // Init first.
    if(self = [self init]){

    ID = [decoder decodeIntForKey:@"ID"];
    GeoLat = [decoder decodeDoubleForKey:@"GeoLat"];
    GeoLong = [decoder decodeDoubleForKey:@"GeoLong"];
    Name = [decoder decodeObjectForKey:@"Name"];
    NSLog(@"Name in decode: %@", Name); //WORKS! logs the name

    Address = [decoder decodeObjectForKey:@"Address"];
    Phone = [decoder decodeObjectForKey:@"Phone"];
    }

    return self;
}

- (void)dealloc
{
    [Name release];
    [ID release];
    [Address release];
    [Phone release];


    [super dealloc];
}
@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
            }
        }
    }

感觉我什么都试过了,但无法弄清楚它在解码中是如何工作的,但在我将它放入数组后循环数组时却不知道。

有人有什么想法吗?

最佳答案

您没有保留 initWithCoder 中的属性。

Name = [decoder decodeObjectForKey:@"Name"];

没有使用您定义的(保留)属性的 setter 。你只是在设置伊娃。这意味着您不会获得所有权,它可以被解除分配。

您可以通过以下两种方式保留案例中的属性:

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

关于iphone - NSCoding with as NSString inside a object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3467951/

相关文章:

ios - 选择最初显示的 ViewController

objective-c - 在 iOS 应用程序中处理数据(选择什么?NSData、CoreData、sqlite、PList、NSUserDefaults)

ios - NSUserDefaults initWithSuiteName 在删除应用程序后仍然存在

ios - 如何使用 Apple 的数据保护 API 加密我的数据?

iphone - 如何显示 UIMenuController 到 UIBarButtonItem

ios - 使用专用类用 iPhone 模拟信标

c++ - 将 Objective C 格式的代码转换为纯 C++

IOS Twilio IP 消息系统初始集成步骤

html - Mailchimp 嵌入表格 : too many recent signup requests on iOS 10 devices

iphone - FBConnect facebook.stream.publish 与 NSDictionary 问题