Json serialize array in objective-c - iphone

I want to serialize array to JSON.
Here is my JSON -
{
"User":{"Id":"1222","Email":"asdad#adasd.com"},
"Person":{"Name":"John","Surname":"Smith"}
}
values 1222, asdad#adasd.com, John, Smith are examples. this values will be define in controller.
I don't know how to handle serialize this arrays inside JSON.
I need to send objects - user and person to the server with different values, depending on the user. This is my model. Both are arrays.
Here is my code
Header
#import <Foundation/Foundation.h>
#import "BaseRequest.h"
#interface SaveUserProfileRequest : BaseRequest {
NSMutableArray *_user;
NSMutableArray *_person;
NSMutableArray *_address;
NSString *_userId;
NSString *_userEmail;
NSString *_userName;
NSString *_userSurname;
}
- (id)initWithUser:(NSMutableArray*)user andPerson:(NSMutableArray*)person andAddress:(NSMutableArray*)address andUserId:(NSString*)userId andUserEmail:(NSString*)userEmail andUserName:(NSString*)userName andUserSurname:(NSString*)userSurname;
#property (nonatomic, strong) NSMutableArray* user;
#property (nonatomic, strong) NSMutableArray* person;
#property (nonatomic, strong) NSMutableArray* address;
#property (nonatomic, strong) NSString* userId;
#property (nonatomic, strong) NSString* userEmail;
#property (nonatomic, strong) NSString* userName;
#property (nonatomic, strong) NSString* userSurname;
#end
Implementation
#import "SaveUserProfileRequest.h"
#import "SaveUserProfileResponse.h"
#import "OrderedDictionary.h"
#import "BaseData.h"
#implementation SaveUserProfileRequest
#synthesize user=_user, person=_person, address=_address, userId=_userId, userEmail=_userEmail, userName=_userName, userSurname=_userSurname;
- (id)initWithUser:(NSMutableArray*)user andPerson:(NSMutableArray*)person andAddress:(NSMutableArray*)address andUserId:(NSString*)userId andUserEmail:(NSString*)userEmail andUserName:(NSString*)userName andUserSurname:(NSString*)userSurname; {
self = [super init];
if(self){
self.user = user;
self.person = person;
self.address = address;
self.userId = userId;
self.userEmail = userEmail;
self.userName = userName;
self.userSurname = userSurname;
}
return self;
}
- (NSDictionary*) serialize{
OrderedDictionary *sup = [OrderedDictionary dictionaryWithCapacity:3];
[sup setValue:self.user forKey:#"User"];
[sup setValue:self.person forKey:#"Person"];
NSArray *users = [OrderedDictionary valueForKey:#"User"];
_user = [NSMutableArray arrayWithCapacity:[users count]];
for(NSDictionary *user in users){
OrderedDictionary *userDict = [OrderedDictionary dictionaryWithCapacity:2];
[userDict setValue:self.userId forKey:#"Id"];
[userDict setValue:self.userEmail forKey:#"Mail"];
return userDict;
}
NSArray *persons = [OrderedDictionary valueForKey:#"Person"];
_person = [NSMutableArray arrayWithCapacity:[persons count]];
for(NSDictionary *person in persons){
OrderedDictionary *personsDict = [OrderedDictionary dictionaryWithCapacity:2];
[personsDict setValue:self.userName forKey:#"Name"];
[personsDict setValue:self.userSurname forKey:#"Surname"];
return personsDict;
}
return sup;
}
Please, help me a little
Every little hint will be really appreciate.
Thanks !

For handeling JSON in Objective-C, I always use JSONKit. You can fork it here: https://github.com/johnezang/JSONKit
Some examples on how it can be used:
NSArray *array = #[..];
NSString *arrayAsJsonString = [array JSONString];
NSString *string = #"..";
id objectFromJsonString = [string objectFromJSONString]; // i.e an NSArray or NSDictionary

If your app is for iOS 5 or greater, you can use NSJSONSerialization:
http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html
Otherwise I recommend SBJson.

Related

core data fetching the information in the array objective-c

I'm quite new to Objective-C. I've been trying to fetch from my core data entity. The following code fetches the right row because it only returns 1 result.
but when I try to NSlog() it to see its values what I get is:
iGym[3922:c07] (
"<User: 0x8397690> (entity: User; id: 0x8341580 <x-coredata://114815EF-85F4-411F-925B-8479E1A94770/User/p19> ; data: <fault>)"
)
I am used to PHP that I just do a var_dump() and i get all the information in the array... specially when I am expecting 16 results as this entity has 16 fields.
Could anyone tell me how could I inspect that array?
and also, most important, how do I ultimately do this: fetch the Gender field of the matched email field and assign it to a NSString variable.
The query i want to do in sql is SELECT Gender FROM myTable WHERE email = "something#something.com;
-(NSInteger*)selectedGenderMethod
{
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:#"User" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
request.predicate = [NSPredicate predicateWithFormat:#"email = %#",_currentUser];
NSError *error = nil;
NSArray *matches = [[context executeFetchRequest:request error:&error] mutableCopy];
NSString * someVar= [matches[0] objectForKey:#"email"];
NSLog(#"%#",someVar);
//More to come once this is sorted
return 0;
}
This fetching code is happening in my genderPickerViewController : UIViewController
NSLog(#"%#", matches[0]);
returns
<User: 0x83a6b00> (entity: User; id: 0x83995f0 <x-coredata://114815EF-85F4-411F-925B-8479E1A94770/User/p19> ; data: <fault>)
This is my User.h:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#interface User : NSManagedObject
#property (nonatomic, retain) NSDate * dob;
#property (nonatomic, retain) NSString * email;
#property (nonatomic, retain) NSNumber * firstTime;
#property (nonatomic, retain) NSString * gender;
#property (nonatomic, retain) NSString * height;
#property (nonatomic, retain) NSNumber * idFB;
#property (nonatomic, retain) NSNumber * idUserExternal;
#property (nonatomic, retain) NSNumber * idUserInternal;
#property (nonatomic, retain) NSNumber * isPT;
#property (nonatomic, retain) NSString * language;
#property (nonatomic, retain) NSString * metricSystem;
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) NSString * nickname;
#property (nonatomic, retain) NSString * password;
#property (nonatomic, retain) NSString * surname;
#property (nonatomic, retain) NSString * weight;
#end
User.m:
#implementation User
#dynamic dob;
#dynamic email;
#dynamic firstTime;
#dynamic gender;
#dynamic height;
#dynamic idFB;
#dynamic idUserExternal;
#dynamic idUserInternal;
#dynamic isPT;
#dynamic language;
#dynamic metricSystem;
#dynamic name;
#dynamic nickname;
#dynamic password;
#dynamic surname;
#dynamic weight;
#end
Is the user object a NSDictionary or an NSArray by chance? If that is the case you are simply logging out the object, you will need to specify a specific entity in the object to output.
For example if it's a NSDictionary
NSString *name = [matches[0] objectForKey:#"name"];
NSLog("Name %#", name);
You could also try
NSString *email = (User *)matches[0].email;
NSLog("Email %#", email);
What you are doing is right, you usually get the full objects and not only one attribute, Core Data is an object storage.
So you only need [((User *)matches[0]) gender] to get the gender of the returned user.
If you want to get some extra information from objects you must implement:
- (NSString *)description
It is used for converting to string and logging.
You can use the following code,
[NSPredicate predicateWithFormat:#"email CONTAINS[c] %#", _currentUser];
OR
[NSPredicate predicateWithFormat:#"email CONTAINS[cd] %#", _currentUser];
then you can print the fetched results as type po OBJECT_NAME.PROPERTY in debug area

How do I get out data from my NSDirectory (not just a property typo)?

I'm missing something simple I think, but been at it for days now without solving this. Even Started to create a "work-around" just to solve it for now, but still want to solve this the "right" way. Any suggestions? Thank's!
.
The problem:
Seems to be missing the class Adealer (get error "-[Adealer objectAtIndex:]: unrecognized selector sent to instance 0x8c5f8b0"), but I did the import Adealer.h to this "detailsVC". But it's not just a simple error of naming the property wrong (objectForKey:#"CustName" instead of "custname" etc - tested this a lot).
Also, I've got similar "listVC"s without a class like Adealer in them, that also transfer data the same way to the same "detailsVC" and they work just fine! Then I just get the data with calls like;
self.labelRestName.text = [restDetails objectForKey:#"CustName"];
Overview:
I got a tableViewController "listVC" that creates the data and show a list, then a ViewController "detailsVC" to show the details. The data (selected row object in "listVC" is transfered via a seque and "destVC.restGPSTransfer" (NSDictionary). The data arrives ok in the "detailsVC" and looks like this in the terminal;
dealerName = Uppsala Centrum Test
dealerAdressStreet = Dragarbrunnsgatan 55
dealerAdressZip = 75320
dealerAdressCity = Uppsala
dealerLongitude = 17.63893
dealerLatitude = 59.85856
dealerDistance2 = 8586398.000000
etc
.
Following the data:
"listVC"
1) First fetching data from web via a AFNetworking json object into an NSMutableArray "restFeed" - ok.
2) Then creating my own data to an NSMutableArray within this loop into a NSMutableArray "updatedDealers" - ok;
NSMutableArray *updatedDealers = [[NSMutableArray alloc]init];
while (i+1 < [_restFeed count]) {
i++;
// Get dealer position function here
// Get distance function here
// Then create my own data here (also #imported Adealer to "listVC";
Adealer *theDealer = [[Adealer alloc]init];
theDealer.dealerName = [[_restFeed objectAtIndex:i]objectForKey:#"CustName"];
theDealer.dealerLongitude = [[_restFeed objectAtIndex:i]objectForKey:#"long"];
theDealer.dealerLatitude = [[_restFeed objectAtIndex:i]objectForKey:#"lat"];
theDealer.dealerDistance2 = theDistance;
// etc...
// Check if data ok
NSLog(#"theDealer = %#",[theDealer description]);
// Don't add dealer object without positiondata to the new array
if (![theDealer.dealerLatitude isEqualToString:#""]) {
[updatedDealers addObject:theDealer];
}
3) Then I use NSSortdescriptor to sort the dealers in NSMutableArray "updatedDealers" into distance order and finally creates the new NSMutableArray "restFeed" with this; (also did "#synthesize dealerFeed = _dealerFeed;" in "listVC") - ok.
_dealerFeed = [NSMutableArray arrayWithArray:sortedContestArray];
4) The populating some tableViewCells with this array and it works just fine - ok.;
cell.cellDealerName.text = [NSString stringWithFormat:#"%#",[[_dealerFeed objectAtIndex:indexPath.row]dealerName]];
5) In the function didSelectRowAtIndexPath transfer the selected object with the "detailsVC"'s NSDictionary "restGPSTransfer" - ok;
destVC.restGPSTransfer = [_dealerFeed objectAtIndex:myIndexPath.row];
"detailsVC":
6) The data seems to transfer ok (se top of this post) but when trying to call the data with;
self.labelRestName.text = [restGPSTransfer objectForKey:#"dealerName"];
I get this error and the app crashes: "-[Adealer objectAtIndex:]: unrecognized selector sent to instance 0x8c5f8b0".
Some more testing done...
Tried to verify the structure + it's keys and properties of the NSDictionary "restGPSTransfer", but using description only got me so far. And have not solved my problem and I still get the "unrecognized selector" error. Could it maybe have become dictionaries within dictionary's or something?
Constructed this little simple if-test to see if the property is really there. But I have to check every property "manually". There's propably a smarter way to check the hole NSDictionary / NSArray?
if ([restGPSTransfer objectForKey:#"dealerName"]) {
NSLog(#"= YES! key exists.");
} else {
NSLog(#"= Nope! key don't exists");
}
THANK'S for any help on this :-)
.
UPDATE the Adealer class files;
Adealer.h
#import <Foundation/Foundation.h>
#interface Adealer : NSObject
#property (nonatomic, retain) NSString * dealerName;
#property (nonatomic, retain) NSString * dealerAdressCity;
#property (nonatomic, retain) NSString * dealerAdressStreet;
#property (nonatomic, retain) NSString * dealerAdressZip;
#property (nonatomic, retain) NSNumber * dealerID;
#property (nonatomic, retain) NSString * dealerImages;
#property (nonatomic, retain) NSString * dealerLogo;
#property (nonatomic, retain) NSString * dealerMail;
#property (nonatomic, retain) NSString * dealerProducts;
#property (nonatomic, retain) NSString * dealerTel;
#property (nonatomic, retain) NSString * dealerText;
#property (nonatomic, retain) NSString * dealerWeb;
#property (nonatomic, retain) NSString * dealerLongitude;
#property (nonatomic, retain) NSString * dealerLatitude;
#property (nonatomic, retain) NSString *dealerDistance;
#property float dealerDistance2;
#end
Adealer.m
#import "Adealer.h"
#implementation Adealer
#synthesize dealerAdressCity, dealerAdressStreet, dealerAdressZip, dealerID, dealerImages, dealerLogo;
#synthesize dealerMail, dealerName, dealerProducts, dealerTel, dealerText, dealerWeb;
#synthesize dealerLongitude, dealerLatitude, dealerDistance,dealerDistance2;
- (NSString *)description {
// Added extension of description
NSMutableString *string = [NSMutableString string];
[string appendString:#"\ntheDealer object and it's properties:\n"];
[string appendFormat:#"dealerName = %#\n", dealerName];
[string appendFormat:#"dealerAdressStreet = %#\n", dealerAdressStreet];
[string appendFormat:#"dealerAdressZip = %#\n", dealerAdressZip];
[string appendFormat:#"dealerAdressCity = %#\n", dealerAdressCity];
[string appendFormat:#"dealerTel = %#\n", dealerTel];
[string appendFormat:#"dealerMail = %#\n", dealerMail];
[string appendFormat:#"dealerWeb = %#\n", dealerWeb];
[string appendFormat:#"dealerLogo = %#\n", dealerLogo];
[string appendFormat:#"dealerImages = %#\n", dealerImages];
[string appendFormat:#"dealerText = %#\n", dealerText];
[string appendFormat:#"dealerProducts = %#\n", dealerProducts];
[string appendFormat:#"dealerLongitude = %#\n", dealerLongitude];
[string appendFormat:#"dealerLatitude = %#\n", dealerLatitude];
[string appendFormat:#"dealerDistance = %#\n", dealerDistance];
[string appendFormat:#"dealerDistance2 = %f\n\n", dealerDistance2];
return string;
}
#end
SOLVED!
Posted if anyone else needs it here.
The solution
In my "detailsVC" I first did this iVar declaration;
.h:
Adealer *theDealer;
#property (nonatomic, retain) Adealer *theDealer;
.m:
#synthesize theDealer;
Then in my "listVC" i did this to transfer the Adealer object and it's properties to the "detailsVC" (remember that the Adealer object already has got it's properties earlier in the described "loop");
Instead of my earlier;
destVC.restGPSTransfer = [_dealerFeed objectAtIndex:myIndexPath.row];
I changed it to;
destVC.theDealer = [_dealerFeed objectAtIndex:myIndexPath.row];
And to actually show and check the transferred property in "detailsVC" I can now simply call this to get the dealers name (or any other properties);
self.labelRestName.text = theDealer.dealerName;
NSLog(#"theDealer.name = %#",theDealer.dealerName);
Works great! Happy Coding everyone!

Remove particular objects from an array based on objects from another array

Setup: Have a UITableView which shows US golf courses with name, street, state etc.
UITableView's data source is a NSMutableArray of objects from my class GolfCourse called allGolfCourses.
Now I like to remove all west coast golf courses from allGolfCourses and create a new array called eastCoastGolfCourses. I have another NSArray with string objects of all west coast states (Abbreviations) called westCoastStates but having a hard time connecting these two.
How do I iterate through allGolfCourses and remove all objects which have a state Abbreviations found in westCoastStates array?
westCoastStates Array:
self.westCoastStates = [NSMutableArray arrayWithObjects:
#"CH",
#"OR",
#"WA",
nil];
GolfCourse.h
#interface GolfCourse : NSObject
#property (nonatomic, strong) NSString *longitude;
#property (nonatomic, strong) NSString *latitude;
#property (nonatomic, strong) NSString *clubName;
#property (nonatomic, strong) NSString *state;
#property (nonatomic, strong) NSString *courseInfo;
#property (nonatomic, strong) NSString *street;
#property (nonatomic, strong) NSString *city;
#property (nonatomic, strong) NSString *clubID;
#property (nonatomic, strong) NSString *phone;
#end
Note: NSString *state; contains the state abbreviation for example: FL
I know how to do this with a single argument but don't know how to check against all strings from westCoastStates array. Hope you can help.
How about?
NSSet* westCoastStatesSet = [NSSet setWithArray:self.westCoastStates];
NSIndexSet* eastCoastGolfCoursesIndexSet = [allGolfCourses indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
GolfCourse* course = (GolfCourse*)obj;
if ([westCoastStatesSet containsObject:course.state]) {
return NO;
}
return YES;
}];
NSArray* eastCoastGolfCourses = [allGolfCourses objectsAtIndexes:eastCoastGolfCoursesIndexSet];
Update: I believe this could be condensed with the use of predicates
NSPredicate *inPredicate = [NSPredicate predicateWithFormat: #"!(state IN %#)", self.westCoastStates];
NSArray* eastCoastGolfCourses = [allGolfCourses filteredArrayUsingPredicate:inPredicate];
Pseudo-code:
for (int i = 0; i < allGolfCourses.length;) {
Course* course = [allGolfCourses objectAtIndex:i];
if (<is course in one of the "bad" states?>) {
[allGolfCourse removeObjectAtIndex:i];
}
else {
i++;
}
}
You can quickly iterate on an array like this:
[self.allGolfCourses enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
GolfCourse *currentGolfCourse = (GolfCourse *)obj;
if(![self.westCoastStates containsObject:currentGolfCourse.state]){
[self.eastCoastStates addObject:currentGolfCourse];
}
}];

using an NSArray in a custom class isn't working

Starting over. I am fairly new to objective C. I have created the following class and I can't figure out how to initialize the array.
Can anyone provide any guidance on how to initialize the NSArray?
StatusPost.m
#import "StatusPost.h"
#implementation StatusPost
#synthesize messageId, fromName, friendId, message, choice2, choice3, choice4, picture, fbImage, commentCount, commentArray;
-(id)initWithMessageId:(NSString*) rMessageId
fromName:(NSString*) rFromName
friendId:(NSString*) rFriendId
message:(NSString*) rMessage
choice2:(NSString*) rChoice2
choice3:(NSString*) rChoice3
choice4:(NSString*) rChoice4
picture:(NSString *) rPicture
fbImage:(UIImage *)rfbImage
commentCount:(NSString*) rCommentCount
commentArray:(NSArray*) rCommentArray
{
if (self = [super init]) {
commentArray = [NSArray new];
self.messageId = rMessageId;
self.fromName = rFromName;
self.friendId = rFriendId;
self.message = rMessage;
self.choice2 = rChoice2;
self.choice3 = rChoice3;
self.choice4 = rChoice4;
self.picture = rPicture;
self.fbImage = rfbImage;
self.commentCount = rCommentCount;
self.commentArray = rCommentArray;
}
return self;
}
- (void)dealloc {
[messageId release];
[fromName release];
[friendId release];
[message release];
[picture release];
[fbImage release];
[commentCount release];
[commentArray release];
[super dealloc];
}
#end
StatusPost.h:
#import
#interface StatusPost : NSObject {
NSString* messageId;
NSString* fromName;
NSString* friendId;
NSString* message;
NSString* choice2;
NSString* choice3;
NSString* choice4;
NSString* picture;
UIImage* fbImage;
NSString* commentCount;
NSArray* commentArray;
}
#property (nonatomic, retain) NSString* messageId;
#property (nonatomic, retain) NSString* fromName;
#property (nonatomic, retain) NSString* friendId;
#property (nonatomic, retain) NSString* message;
#property (nonatomic, retain) NSString* choice2;
#property (nonatomic, retain) NSString* choice3;
#property (nonatomic, retain) NSString* choice4;
#property (nonatomic, retain) NSString* picture;
#property (nonatomic, retain) UIImage* fbImage;
#property (nonatomic, retain) NSString* commentCount;
#property (nonatomic, retain) NSArray* commentArray;
-(id)initWithMessageId:(NSString*) rMessageId
fromName:(NSString*) rFromName
friendId:(NSString*) rFriendId
message:(NSString*) rMessage
choice2:(NSString*) rChoice2
choice3:(NSString*) rChoice3
choice4:(NSString*) rChoice4
picture:(NSString*) rPicture
fbImage:(UIImage*) rfbImage
commentCount:(NSString*) rCommentCount
commentArray:(NSArray*) rCommentArray;
#end
It is likely that you aren't ever initializing the array, so when you try to add an object, you are just sending a message to nil. In the custom class's init method, add the line:
commentArray = [NSMutableArray new];
[NSArray new] is shorthand for [[NSArray alloc] init], so, technically speaking, that statement "inits" the NSArray.
However, your code looks a bit peculiar. You have the following statements in your init:
commentArray = [NSArray new];
self.commentArray = rCommentArray;
The first statement is setting the instance variable commentArray to the address of the newly alloced/inited NSArray while the second is setting the property commentArray to a parameter value. However, you have (through the #synthesize) made the instance variable commentArray the "backing store" for the property commentArray, so when you execute the second line the effect of the first line is overwritten (and the NSArray you created is "leaked").
(But if your real question is how to "load" an NSArray with values, you should ask that question -- and you'll get different answers.)

App hanging/crashing after adding NSSet of entities with relationships

I have to main issues that I believe are related as they both occur on the same line of code.
Data Model
NB: I have simplified the code and model as best I can.
I have 3 entities in my Core data model.
Merchant (can have many Branches, can have many Sectors)
Sector (can have many Merchants)
Branch (can have one Merchant)
Data is downloaded (in JSON) to the app. Each Merchant is iterated over sectors are extracted, if the sector exists it is fetched and added to a NSMutableArray.
...
//Iterating through Merchants
...
for(NSDictionary *sector in sectors) {
NSLog(#"\tfetch sectors ID %#", [sector objectForKey:#"sector_id"]);
NSPredicate *sectorPredicate = [NSPredicate predicateWithFormat:#"%K == %d", #"sectorID", [[sector objectForKey:#"sector_id"] integerValue]];
[sectorRequest setPredicate:sectorPredicate];
NSArray *existingSector = [self.managedObjectContext executeFetchRequest:sectorRequest error:&error];
if(!error && [existingSector count] == 1) {
NSLog(#"\tfound sector");
[merchantSectors addObject:[existingSector objectAtIndex:0]];
}
else {
NSLog(#"\tcreate a new sector");
//Create a new sector
Sector *newSector = [[Sector alloc] initWithEntity:sectorEntity insertIntoManagedObjectContext:self.managedObjectContext];
newSector.sectorID = [NSNumber numberWithInteger:[[sector objectForKey:#"sector_id"] integerValue]];
newSector.name = [sector objectForKey:#"name"];
[merchantSectors addObject:newSector];
[newSector release]; newSector = nil;
}
}
[sectorRequest release]; sectorRequest = nil;
NSLog(#"\tadd sectors to merchant");
[currentMerchant addSector:merchantSectors]; //<---- crash and hang
The App will either hang at:
[currentMerchant addSector:merchantSectors];
or sometimes throw an exception:
*** Terminating app due to uncaught exception \
'NSInternalInconsistencyException', reason: \
'-[__NSCFSet addObject:]: mutating method sent to immutable object'
The Branch parsing code is almost identical but never has these issues or the app will hang or crash before it becomes an issue (??).
If the App is deleted and reinstalled the code will work fine, is it possible that existing identical relationships are causing this problem?
Edit: The parsing of the JSON is called using an NSInvocationOperation, so when it hangs the interface stays responsive. The crash version kills the app.
Edit 2: Merchant.h and Merchant.m
Merchant.h
#import <CoreData/CoreData.h>
#class Branch;
#class Sector;
#interface Merchant : NSManagedObject
{
}
#property (nonatomic, retain) NSString * street;
#property (nonatomic, retain) NSString * locality;
#property (nonatomic, retain) NSString * city;
#property (nonatomic, retain) NSNumber * merchantID;
#property (nonatomic, retain) NSString * postcode;
#property (nonatomic, retain) NSString * property;
#property (nonatomic, retain) NSString * organisation;
#property (nonatomic, retain) NSDate * expires;
#property (nonatomic, retain) NSSet * Branch;
#property (nonatomic, retain) NSSet* Sector;
#end
#interface Merchant (CoreDataGeneratedAccessors)
- (void)addBranchObject:(Branch *)value;
- (void)removeBranchObject:(Branch *)value;
- (void)addBranch:(NSSet *)value;
- (void)removeBranch:(NSSet *)value;
- (void)addSectorObject:(Sector *)value;
- (void)removeSectorObject:(Sector *)value;
- (void)addSector:(NSSet *)value;
- (void)removeSector:(NSSet *)value;
#end
Merchant.m
#import "Merchant.h"
#import "Branch.h"
#implementation Merchant
#dynamic street;
#dynamic locality;
#dynamic city;
#dynamic merchantID;
#dynamic postcode;
#dynamic property;
#dynamic organisation;
#dynamic expires;
#dynamic Branch;
#dynamic Sector;
#end
Try to add Sectors to Merchant one by one using CoreData add<Key>Object: and remove<Key>Object: auto-generated methods (as described in Custom To-Many Relationship Accessor Methods)
for(NSDictionary *sector in sectors) {
NSPredicate *sectorPredicate = [NSPredicate predicateWithFormat:#"%K == %d", #"sectorID", [[sector objectForKey:#"sector_id"] integerValue]];
[sectorRequest setPredicate:sectorPredicate];
NSArray *existingSector = [self.managedObjectContext executeFetchRequest:sectorRequest error:&error];
if(!error && [existingSector count] == 1)
{
[currentMerchant addSectorObject:[existingSector lastObject]];
}
else
{
Sector *newSector = [[Sector alloc] initWithEntity:sectorEntity insertIntoManagedObjectContext:self.managedObjectContext];
newSector.sectorID = [NSNumber numberWithInteger:[[sector objectForKey:#"sector_id"] integerValue]];
newSector.name = [sector objectForKey:#"name"];
[currentMerchant addSectorObject:newSector];
[newSector release];
}
}
Or you can retrieve mutable proxy object contains currentMerchants's sectors via mutableSetValueForKey: and add sectors to it:
NSMutableSet *merchantSectors = [currentMerchant mutableSetValueForKey:#"sector"];
for(NSDictionary *sector in sectors) {
NSPredicate *sectorPredicate = [NSPredicate predicateWithFormat:#"%K == %d", #"sectorID", [[sector objectForKey:#"sector_id"] integerValue]];
[sectorRequest setPredicate:sectorPredicate];
NSArray *existingSector = [self.managedObjectContext executeFetchRequest:sectorRequest error:&error];
if(!error && [existingSector count] == 1)
{
[merchantSectors addObject:[existingSector lastObject]];
}
else
{
Sector *newSector = [[Sector alloc] initWithEntity:sectorEntity insertIntoManagedObjectContext:self.managedObjectContext];
newSector.sectorID = [NSNumber numberWithInteger:[[sector objectForKey:#"sector_id"] integerValue]];
newSector.name = [sector objectForKey:#"name"];
[merchantSectors addObject:newSector];
[newSector release];
}
}
Anyway, for convenience it's better to use lowercase sectors name for Mecrhant entity for to-many relationship with Sector entity: lowercase not to be ambiguous with Sector class name, and with s at and to be sure, that getter methods for this property return multiple objects.