0

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

我尝试使用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
4

2 回答 2

0

您需要确保您的自定义对象实现了 NSCoder 类。NSCoder 抽象类声明了具体子类用来在内存和其他格式之间传输对象和其他 Objective-C 数据项的接口。

这是一个向您展示如何操作的教程。

于 2013-04-05T05:34:21.600 回答
0

试试看....

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

NSString *myValue = [[NSUserDefaults standardUserDefaults]valueForKey:@"myKey"];//retrive value 
NSLog(@"%@",myValue);
于 2013-04-05T05:46:27.250 回答