I am using NSMutableDictionary. Declare it using property as follows:
#property (strong, nonatomic) NSMutableDictionary *books;
In the method, I am assigning it values.
- (void)setUpBooks {
if (self.books == nil){
self.books = [[NSMutableDictionary alloc] init];
}
// my code goes here...
[self.books setObject:book forKey:key];
NSLog(#"books : %#",self.books);
NSLog(#"books Count : %d",[self.books count]);
}
In this NSLog it's showing correct values. But when I tried to use self.books in another method its showing null. I don't know where my data is losing.
Easy way to use NSMutableDictionay.
Try this,
NSMutableDictionary * books = [[NSMutableDictionary alloc] init];
NSDictionary *book = [[NSDictionary alloc] initWithObjectsAndKeys:#"Value",#"BOOK_KEY", nil];
[books addEntriesFromDictionary:book];
Hope it's help you.
Related
Is there anyway on [randomSelection addObject:renderView]; assign custom peerID references (such as id: cgiLoC0OdMNtVph3aMZV or cxeeeLoC0OdMNtVph3aMZV), for later to query from some random/unsorted UIView's?
For example: while create i have such as below:
//#property (copy, nonatomic) NSMutableArray *randomSelection;
//UIView *renderView;
//renderView = [[UIView alloc] initWithFrame:CGRectMake(padding_x, padding_y, 50, 50)];
NSMutableArray *randomSelection = [[NSMutableArray alloc] init];
[randomSelection addObject:renderView];
//self.randomSelection = randomSelection;
But there are some situation when i need to remove some of the UIView but in that case i only have some peerID references. How do i have some custom reference while using NSMutableArray
- (void)callingRemoveMethod:(NSString *)peerID {
NSLog(#"removed %#", peerID);
/* instead of objectAtIndex is there a way to query
it by any custom references? */
//UIView *test = [self.randomSelection objectAtIndex:0];
//test.backgroundColor = [UIColor redColor];
}
You could use an NSMutableDictionary instead and use your ID as the key to the dictionary.
UIView has a tag property (an int) that you can use for this purpose, but it can't be arbitrary information (just an int)
EDIT:
NSMutableDictionary *jobs = [NSMutableDictionary
dictionaryWithDictionary:#{
#"Audi TT" : #"John",
#"Audi Quattro (Black)" : #"Mary",
#"Audi Quattro (Silver)" : #"Bill",
#"Audi A7" : #"Bill"
}];
// Transfer an existing job to Mary
[jobs setObject:#"Mary" forKey:#"Audi TT"];
// Finish a job
[jobs removeObjectForKey:#"Audi A7"];
// Add a new job
jobs[#"Audi R8 GT"] = #"Jack";
why adding string to nsmuarray not work?
firstly, i add the a NSDictionary by keypath to the NSMutableArray,
its work.
after that i want to add one more string to that but its not work.
NSMutableArray *_joinornot;
_joinornot = [[NSMutableArray alloc] init];
NSDictionary *tempobject = [[NSDictionary alloc] init];
_joinornot = [tempobject valueForKeyPath:#"groupid"];
until now everything work.
[_joinornot addObject:#"111"];<----unrecongnized selector sent to instance
if _joinornot = [tempobject valueForKeyPath:#"groupid"]; returns nil, then your array will be nil, and then you cant call addObject. so maybe add a nil check
Looks like "_joinornot" it's not an NSMutableArray or NSMutable data type, try to see what kind of object it is:
NSLog(#"%#", [_joinornot class]);
If it is not a subclass of Mutable type you can't add objects to him.
Try below code:
Before adding object just check for nil.
NSMutableArray *_joinornot;
_joinornot = [[NSMutableArray alloc] init];
NSDictionary *tempobject = [[NSDictionary alloc] init];
_joinornot = [tempobject valueForKeyPath:#"groupid"];
if (_joinornot==nil) {
_joinornot = [[NSMutableArray alloc] init];
[_joinornot addObject:#"111"];
}
else{
[_joinornot addObject:#"111"];
}
Edit:
May be it's converted to NSArray so it will be no more mutable, try with
_joinornot = [[tempobject valueForKeyPath:#"groupid"] mutableCopy];
I would like to use an NSDictionay (I think this is the correct method) to save data to in a class which is accessed by other classes:
.h
#property (retain, nonatomic) NSArray *NameKey;
#property (retain, nonatomic) NSArray *DataFields;
#property (retain, nonatomic) NSDictionary *MDArray;
.m
#synthesize NameKey;
#synthesize DataFields;
#synthesize MDArray;
- (id) init
{
NameKey = [NSArray arrayWithObjects:#"DeviceName",nil];
DataFields = [NSArray arrayWithObjects:#"Serial", nil];
MDArray = [NSDictionary dictionaryWithObjects:DataFields forKeys:NameKey];
}
Couple of questions:
Is this the correct method for setting up an NSDictionary?
How you I add/delete/modify the data. Assuming that the name was unique
Thanks in advance.
Why not use modern syntax:
MDArray = #{ #"DeviceName": #"Serial" }
If you want a mutable version you can send it the mutableCopy message
The way I solved this was to:
devices = [[NSMutableArray alloc] init];
dataFields = [[NSArray alloc] initWithObjects:#"Name",#"Type", nil];
NSArray *addDevice = [[NSArray alloc] initWithObjects:#"MM-TEST",#"-- --", nil];
NSDictionary *infoblock = [[NSMutableDictionary alloc] initWithObjects:addDevice forKeys:dataFields];
[devices addObject:infoblock];
Seems to work fine.
Can someone help me out on this:
Im creating a property in my TableVC.m file :
#property NSMutableArray *savingBeaconSpecs;
In my Viewdidload I instantiate the array:
NSMutableArray *savingBeaconSpecs = [[NSMutableArray alloc]init];
Now I do requests to the server, and I want to save the returned JSON into objects and save these each time in the array. So I did the following in the ConnectionDidFinishLaunching:
self.artworkArray = [NSJSONSerialization JSONObjectWithData:self.data options:0 error:&err];
NSLog(#"Log ArtworkArray in ConnectionDidFinishLoading%#", self.artworkArray);
And:
Artwork *artwork = [[Artwork alloc]init];
artwork.title = [self.artworkArray valueForKey:#"name"];
artwork.artist = [[self.artworkArray objectForKey:#"artist"] valueForKey:#"name"];
artwork.CreationYear = [self.artworkArray valueForKey:#"creationYear"];
artwork.categorie = [[self.artworkArray objectForKey:#"exposition"] valueForKey:#"name"];
Now I want to save this object into the savingBeaconSpecs NSMutableArray
[self.savingBeaconSpecs addObject:artwork];
But the NSMUtableArray savingBeaconSpecs always returns 0 when i try log his content
Anyone please?
Because you declare it locally in your viewDidLoad :
NSMutableArray *savingBeaconSpecs = [[NSMutableArray alloc]init];
you should use
self.savingBeaconSpecs = [[NSMutableArray alloc]init];
and
[self.savingBeaconSpecs addObject:artwork];
and declare your property as (without the first capital S)
#property NSMutableArray *savingBeaconSpecs;
To instantiate the array, you should do:
self.savingBeaconSpecs = [[NSMutableArray alloc] init];
or equally good:
self.savingBeaconSpecs = [NSMutableArray array];
I'm getting data from SQL server in following variables in one class:
#property(retain,nonatomic) NSString* trainingName;
#property(retain,nonatomic) NSNumber* trainingCount;
In other class,I want to append this value in NSMutableArray and finally make a dictonary.I'm doing this as following:
-(void)getTrainingDetails
{
MyTestSp_GetTrainingCountsList *objReturnTrainings = [MyTestSp_GetTrainingCounts findAll];
NSMutableArray *trainingNames = [[NSMutableArray alloc]init];
NSMutableArray *trainingCounts = [[NSMutableArray alloc]init];
NSMutableDictionary *dictTrainings = [[NSMutableDictionary alloc]init];
if ([objReturnTrainings length] > 0)
{
for (MyTestSp_GetTrainingCounts *obj in objReturnTrainings)
{
// get the values and assign to NSMutable array
trainingNames = trainingName;
trainingCounts = trainingCount;
//Make the dictionary.
}
}
}
Is this the correct way of doing and how can I put this in dictionary?
Please help.
You appear to be trying to set your array directly to your number / string (which you aren't actually getting out of obj so your code shouldn't compile...). You also don't need the arrays to create the dictionary. You can just do:
for (MyTestSp_GetTrainingCounts *obj in objReturnTrainings)
{
dictTrainings[obj.trainingName] = obj.trainingCount;
}