如何在iphone SDK中使用NSUserDefaults传递自定义类对象?

时间:2013-04-05 05:24:30

标签: iphone ios objective-c xcode nsuserdefaults

在我的应用中,我有自定义类文件。

我尝试使用ObjectforKey,但它显示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

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

试试吧......

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

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