在我的应用中,我有自定义类文件。
我尝试使用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
答案 0 :(得分:0)
您需要确保自定义对象实现NSCoder类。 The NSCoder abstract class declares the interface used by concrete subclasses to transfer objects and other Objective-C data items between memory and some other format.
答案 1 :(得分:0)
试试吧......
[[NSUserDefaults standardUserDefaults]setValue:@"myValue" forKey:@"myKey"];//set value
NSString *myValue = [[NSUserDefaults standardUserDefaults]valueForKey:@"myKey"];//retrive value
NSLog(@"%@",myValue);