so I've been working with NSNotificationCenter in Xcode and I've tried to attach a NSDictionary to my notification using userInfo.
NSArray *objects = [NSArray arrayWithObjects:#"Example Name", #"Example Description", #"Example Date", nil];
NSArray *keys = [NSArray arrayWithObjects:#"name", #"description", #"date", nil];
NSDictionary *dict = [NSDictionary
dictionaryWithObjects:objects
forKeys:keys];
[[NSNotificationCenter defaultCenter] postNotificationName:#"Notification" object:nil userInfo:dict];
When I try to run the app and post the notification, it crashes at the line:
NSArray *keys = [NSArray arrayWithObjects:#"name", #"description", #"date", nil];
I later found that if the array size exceeded 2 objects, the app would crash.
So if I changed my code to the snippet below, it would work.
NSArray *objects = [NSArray arrayWithObjects:#"Example Name", #"Example Description", nil];
NSArray *keys = [NSArray arrayWithObjects:#"name", #"description", nil];
NSDictionary *dict = [NSDictionary
dictionaryWithObjects:objects
forKeys:keys];
[[NSNotificationCenter defaultCenter] postNotificationName:#"Notification" object:nil userInfo:dict];
Is there any way I can work around this, or am I doing something terribly wrong?
Does this code compile? Try cleaning and rebuilding the project.
Related
I have a class which can be accessed from all other pages just like facebook chat head bubble.Its a cart in my application.Items to the cart could be added from different views. HAve tried with NSNotification and is not working.The array inside the cart view is gettig as null.Any help?What i tried is:
-(IBAction)addToCart:(id)sender{
NSMutableArray *itemToCart=[[NSMutableArray alloc]init];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObject:gearImgView.image forKey:#"equipImage"];
[dict setObject:nameLbl.text forKey:#"name"];
[dict setObject:priceLbl.text forKey:#"price"];
[dict setObject:self.shopifyIDString forKey:#"shopifyID"];
[itemToCart addObject:dict];
[[NSNotificationCenter defaultCenter] postNotificationName:#"equipmentSelected" object:nil userInfo:dict];
}
And in the cart view,
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(loadEquipmentview:) name:#"equipmentSelected" object:nil];
[super viewDidLoad];
}
-(void)loadEquipmentview:(NSNotification *)notification{
NSDictionary *dict = [notification userInfo];
NSString *shopifyID = [dict objectForKey:#"shopifyID"];
NSLog(#"%#",dict);
}
Your array exist just in the addCart method's scope. try like this :
-(IBAction)addToCart:(id)sender{
NSMutableArray *itemToCart=[[NSMutableArray alloc]init];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObject:gearImgView.image forKey:#"equipImage"];
[dict setObject:nameLbl.text forKey:#"name"];
[dict setObject:priceLbl.text forKey:#"price"];
[dict setObject:self.shopifyIDString forKey:#"shopifyID"];
[itemToCart addObject:dict];
// Create an other dictionnary to hold the array of dictionary
NSMutableDictionary *otherDict = [NSMutableDictionary dictionary];
otherDict[#"yourArrayKey"] = itemToCart;
// The you post the otherDict
[[NSNotificationCenter defaultCenter] postNotificationName:#"equipmentSelected" object:nil userInfo:otherDict]; //be careful here, you should post the otherDict and not the dic.
}
and you retrieve your array, you can do like this :
-(void)loadEquipmentview:(NSNotification *)notification{
NSDictionary *dict = [notification userInfo];
NSMutableArray *array = dic[#"yourArrayKey"];
....
}
If you aren't using ARC, you might want to change the following line,
NSDictionary *dict = [notification userInfo];
To;
NSDictionary *dict = [notification userInfo] retain];
I have a small doubt that is I have an NSArray which contains the following 4 objects:
Genesis, 1 Kings, leviticus, 2 Kings
I want to sort this array in dictionary order like i want an expected output like this
1 Kings, 2 Kings, Genesis, leviticus
How can this be achieved?
Go to this link:
https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Collections/Articles/Arrays.html#//apple_ref/doc/uid/20000132-SW5
This is Apple documentation and your problem is solved over there.
Check out the example.
//First create the array of dictionaries
NSString *last = #"lastName";
NSString *first = #"firstName";
NSMutableArray *array = [NSMutableArray array];
NSArray *sortedArray;
NSDictionary *dict;
dict = [NSDictionary dictionaryWithObjectsAndKeys:
#"Jo", first, #"Smith", last, nil];
[array addObject:dict];
dict = [NSDictionary dictionaryWithObjectsAndKeys:
#"Joe", first, #"Smith", last, nil];
[array addObject:dict];
dict = [NSDictionary dictionaryWithObjectsAndKeys:
#"Joe", first, #"Smythe", last, nil];
[array addObject:dict];
dict = [NSDictionary dictionaryWithObjectsAndKeys:
#"Joanne", first, #"Smith", last, nil];
[array addObject:dict];
dict = [NSDictionary dictionaryWithObjectsAndKeys:
#"Robert", first, #"Jones", last, nil];
[array addObject:dict];
//Next we sort the contents of the array by last name then first name
// The results are likely to be shown to a user
// Note the use of the localizedCaseInsensitiveCompare: selector
NSSortDescriptor *lastDescriptor =[[NSSortDescriptor alloc] initWithKey:last
ascending:YES
selector:#selector(localizedCaseInsensitiveCompare:)];
NSSortDescriptor *firstDescriptor =
[[NSSortDescriptor alloc] initWithKey:first
ascending:YES
selector:#selector(localizedCaseInsensitiveCompare:)];
NSArray *descriptors = [NSArray arrayWithObjects:lastDescriptor, firstDescriptor, nil];
sortedArray = [array sortedArrayUsingDescriptors:descriptors];
This code is an example from Apple documentation.
You can sort an array of NSString alphabetically like this:
NSArray *sortedArray = [myArray sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
I am trying to use facebook publish API link this as given here http://developers.facebook.com/docs/reference/api/post/
NSDictionary *privacyDict = [[[NSDictionary alloc] initWithObjectsAndKeys:#"SELF",#"value",nil] autorelease];
SBJSON *jsonWriter = [[SBJSON new] autorelease];
NSString *privacyJSONStr = [jsonWriter stringWithObject:privacyDict];
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Test test", #"message",
privacyJSONStr, #"privacy",
#"http://www.facebook.com/", #"link",
#"This name check where it will show", #"name",
#"Caption for the link", #"caption",
#"Longer description of the link", #"description",
nil];
[facebookObj requestWithMethodName:#"facebook.Stream.publish" andParams:params andHttpMethod:#"POST" andDelegate:self];
It posted successfully, but not like that as I expected
there is no caption, name, description , link etc
it shown as
This type of result I can get by this parameter
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Test test", #"message",
privacyJSONStr, #"privacy",
nil];
What is the mistake, and How we can publish all the parameter
Amit Battan
try this .. its works for me
- (IBAction )publishStream{
SBJSON *jsonWriter = [[SBJSON new] autorelease];
NSDictionary* actionLinks = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
#"Posting From",#"text",#"http://www.google.com/",#"href", nil], nil];
NSString *actionLinksStr = [jsonWriter stringWithObject:actionLinks];
NSLog(#"Action Link String : %#", actionLinksStr);
NSDictionary* attachment = [NSDictionary dictionaryWithObjectsAndKeys:
#"Feed Name", #"name",
#"Feed Caption is here", #"caption",
#"Feed Description is here", #"description",
#"http://www.google.com/", #"href", nil];
NSString *attachmentStr = [jsonWriter stringWithObject:attachment];
NSLog(#"Attachment String : %#", attachmentStr);
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Share on Facebook", #"user_message_prompt",
actionLinksStr, #"action_links",
attachmentStr, #"attachment",
nil];
[facebook dialog:#"facebook.Stream.publish"
andParams:params
andDelegate:self];
}
Can each index of array hold the NSDictionary?
Thank You.
Yes, the value of an NSArray can resolve to an object identifier for an NSDictionary. However, the array doesn't "hold" the NSDictionary, nor can the index of an NSArray be an NSDictionary. An index from an Array is always an integer value.
An NSArray can hold any type of object, so yes, putting an NSDictionary in an NSArray works just fine.
You sure can, here is an example:
NSDictionary *dict1 = [[NSDictionary alloc] initWithObjectsAndKeys:
#"value1", #"key1", #"value2", #"key2", nil];
NSDictionary *dict2 = [[NSDictionary alloc] initWithObjectsAndKeys:
#"Billy", #"Goat", #"Rover", #"Dog", nil];
NSArray *arrayWithDictionaries = [[NSArray alloc] initWithObjects:
dict1, dict2, nil];
[dict1 release];
[dict2 release];
I tried to insert NSDictionary's in a NSMutableDictionary. There is no error but it won't work, it remains empty.
Here's my code:
NSMutableDictionary *einnahmen = [[NSMutableDictionary alloc] initWithCapacity:20];
NSArray *objects = [NSArray arrayWithObjects:
name,
[NSNumber numberWithInt: x],
[NSNumber numberWithInt: y],
[NSNumber numberWithInt: z],
nil];
NSArray *keys = [NSArray arrayWithObjects:
#"name",
#"startJahr",
#"zins",
#"entnahmefaehig",
nil];
NSDictionary *entry = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
[einnahmen setObject:entry forKey:#"name"]; //seems not to work
After [einnahmen setObject:entry the Debugger shows this:
I have solved this problem. The following code was in the wrong init-method:
NSMutableDictionary *einnahmen =
[[NSMutableDictionary alloc] initWithCapacity:20];
The compiler should show a error.