0

In my app, I have custom class file.

I try using ObjectforKey but it shows me null Value & also give error in console like

[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.

How can i pass the object of custom class to another view?

Code for custom class is as follow:

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
  • 1
    Can you be a bit more clear. Any code about how you do the userDefaults stuff. – Satheesh Apr 05 '13 at 05:30
  • I attached Apple Developer documentation link below : 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 – Sung-Pil Lim Apr 05 '13 at 05:33
  • @MidhunMP, but for me, there will be two interfaces in the header file & got the problem for that. – HARSH AGRAWAL Apr 05 '13 at 07:25

2 Answers2

0

You need to make sure that your custom object implements the NSCoder class. 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.

Here is a tutorial that shows you how.

random
  • 8,568
  • 12
  • 50
  • 85
  • From NSUserDefaults' class reference: "The value parameter can be only property list objects: NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. For NSArray and NSDictionary" – zadr Apr 05 '13 at 08:55
0

Try it....

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

NSString *myValue = [[NSUserDefaults standardUserDefaults]valueForKey:@"myKey"];//retrive value 
NSLog(@"%@",myValue);
Chirag Pipaliya
  • 1,281
  • 12
  • 20