2013-04-05 80 views
0

在我的應用程序中,我有自定義類文件。如何使用iphone SDK中的NSUser默認值傳遞自定義類對象?

我嘗試使用​​但它表明我null價值&也給錯誤控制檯類似

[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value '<FSVenue: 0x181898c0>' of class 'FSVenue'. Note that dictionaries and arrays in property lists must also contain only property values.

我怎麼能自定義類的對象傳遞給另一個看法?

代碼自定義類是如下:

FSVenue.h

#import <UIKit/UIKit.h> 
#import <CoreLocation/CoreLocation.h> 
#import <MapKit/MapKit.h> 

@interface FSLocation : NSObject{ 
    CLLocationCoordinate2D _coordinate; 
} 

@property (nonatomic, assign) CLLocationCoordinate2D coordinate; 
@property (nonatomic,strong)NSNumber*distance; 
@property (nonatomic,strong)NSString*address; 

@end 


@interface FSVenue : NSObject<MKAnnotation> 

@property (nonatomic,strong)NSString*name; 
@property (nonatomic,strong)NSString*venueId; 
@property (nonatomic,strong)FSLocation*location; 

@end 

FSVenue.m

#import "FSVenue.h" 


    @implementation FSLocation 


    @end 

    @implementation FSVenue 


    - (id)init 
    { 
     self = [super init]; 
     if (self) { 
      self.location = [[FSLocation alloc]init]; 
     } 
     return self; 
    } 

    -(CLLocationCoordinate2D)coordinate{ 
     return self.location.coordinate; 
    } 

    -(NSString*)title{ 
     return self.name; 
    } 


@end 
+0

您已經嘗試了什麼? – Balu 2013-04-05 05:28:58

+1

你能更清楚些嗎?任何關於你如何做userDefaults的代碼。 – satheeshwaran 2013-04-05 05:30:29

+0

我附上Apple Developer文檔鏈接如下: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DrawColor/Tasks/StoringNSColorInDefaults.html#//apple_ref/doc/uid/20001693 http ://stackoverflow.com/questions/3000220/best-way-to-save-to-nsuserdefaults-for-custom-class – 2013-04-05 05:33:08

回答

0
+0

從NSUserDefaults的類引用:「值參數可以只是屬性列表對象:NSData,NSString,NSNumber,NSDate,NSArray或NSDictionary。對於NSArray和NSDictionary」 – zadr 2013-04-05 08:55:34

0

試試吧....

[[NSUserDefaults standardUserDefaults]setValue:@"myValue" forKey:@"myKey"];//set value 

NSString *myValue = [[NSUserDefaults standardUserDefaults]valueForKey:@"myKey"];//retrive value 
NSLog(@"%@",myValue); 
相關問題