I have a strange problem because "addObject" is working to add an NSString but not to add an NSArray:
NSMutableString *starCatalogEntryValue = [[NSMutableString alloc] init]; // a single string from a catalog
NSMutableArray *starCatalogEntryData = [[NSMutableArray alloc] init]; // array of strings
NSMutableArray *starCatalogData = [[NSMutableArray alloc] init]; // array of arrays
loop i times {
[starCatalogEntryData removeAllObjects];
loop j times {
[starCatalogEntryData addObject:starCatalogEntryValue]; // This works
}
[starCatalogData addObject:starCatalogEntryData]; // This does not work
}
Actually, adding the array starCatalogEntryData works but not properly. I end up with i entries in starCatalogData but they are all equal to the last value of starCatalogEntryData.
The problem is that you reuse startCatalogEntryData over and over. You want this:
NSMutableString *starCatalogEntryValue = [[NSMutableString alloc] init]; // a single string from a catalog
NSMutableArray *starCatalogData = [[NSMutableArray alloc] init]; // array of arrays
loop i times {
NSMutableArray *starCatalogEntryData = [[NSMutableArray alloc] init]; // array of strings
loop j times {
[starCatalogEntryData addObject:starCatalogEntryValue]; // This works
}
[starCatalogData addObject:starCatalogEntryData]; // This does not work
}
This creates a new array each time.
Related
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];
Hi I'm trying to loop through an array of objects in an array to try and assign them an value. Im doing this through fast enumeration but when I run the build it succeeds but crashes and points to this line:
for (SKSpriteNode* var in objects_array)
. Did i mess up the syntax? I'm still new to objective-c. It tells me that var is unused and when i run the build that point is a breakpoint.
(btw I didn't include the code for when I created the actual SKSpriteNode objects left middle and right because they have multiple properties and i thought it may be distracting. I can post it though if needed)
Thanks!
NSMutableArray* objects_array = [[NSMutableArray alloc] init];
NSMutableArray* value_array = [[NSMutableArray alloc] init];
[objects_array addObject:#"left"];
[objects_array addObject:#"middle"];
[objects_array addObject:#"right"];
for (SKSpriteNode* var in objects_array) {
int value =arc4random_uniform(1);
[value_array addObject:[NSNumber numberWithInt:value]];
}
You have two problems. The first one was said by Rory; you forgot to initialize your variables:
NSMutableArray* objects_array = [[NSMutableArray alloc] init];
NSMutableArray* value_array = [[NSMutableArray alloc] init];
The second problem is that you are working with different variables. Here you say that you are working with NSString*:
[objects_array addObject:#"left"];
[objects_array addObject:#"middle"];
[objects_array addObject:#"right"];
And here you say that you are working with SKSpriteNode*:
for (SKSpriteNode* var in objects_array)
If your sprites are called left, middle and right, you should do this to initialize them (for example):
[SKSpriteNode spriteNodeWithImageNamed:#"left.png"];
And in that case, that should be your for:
for (NSString* var in objects_array)
You need to initialise the arrays:
NSMutableArray* objects_array = [[NSMutableArray alloc] init];
NSMutableArray* value_array = [[NSMutableArray alloc] init];
I have an array that contains movie objects. These objects are stored in a movie array. My movie object is below.
Movie.h
NSString * name;
NSString * cat_name;
I want to add my original array to a UITableView with dynamic rows and sections but I'm finding it difficult. I think the best way to do this is by having an array of arrays.
For example, there would be an array that contains all horror movies, an array that contains all fiction etc. All in one array. I think that would allow me to get the desired end product. I'm finding it difficult code it though.
EDIT The content of the array is dynamic, so I will not know how many objects will be in it at launch (it's being parsed from JSON). So I need to dynamically create the right amount of sections etc.
NSMutableDictionary * mainDictionary = [[NSMutableDictionary alloc] init];
Movie * firstHorrorMovie = [[Movie alloc] init];
firstHorrorMovie.name = #"Psycho";
firstHorrorMovie.cat_name = #"Horror";
Movie * secondHorrorMovie = [[Movie alloc] init];
secondHorrorMovie.name = #"Paranormal Activity";
secondHorrorMovie.cat_name = #"Horror";
Movie * comedyMovie = [[Movie alloc] init];
comedyMovie.name = #"The new guy";
comedyMovie.cat_name = #"Comedy";
NSArray * horrorMovies = [NSArray arrayWithObjects:firstHorrorMovie, secondHorrorMovie, nil];
NSArray * comedyMovies = [NSArray arrayWithObjects:comedyMovie, nil];
[mainDictionary setValue:horrorMovies forKey:#"Horror"];
[mainDictionary setValue:comedyMovies forKey:#"Comedy"];
OR (in your case - dynamically)
NSMutableDictionary * anotherMainDictionary = [[NSMutableDictionary alloc] init];
NSArray * array = [NSArray arrayWithObjects:firstHorrorMovie, secondHorrorMovie, comedyMovie, nil];
for (Movie * movie in array) {
NSMutableArray * array = [anotherMainDictionary valueForKey:movie.cat_name];
if (array) {
[array addObject:movie];
[anotherMainDictionary setValue:array forKey:movie.cat_name];
} else {
NSMutableArray * newArray = [NSMutableArray arrayWithObject:movie];
[anotherMainDictionary setValue:newArray forKey:movie.cat_name];
}
}
I am trying to store an array in a NSMutableDictionary. However the NSMutableDictionary is null after i have set objects to it. Here is my code any help is appreciated:
NSMutableArray *arrTemp = [[NSMutableArray alloc] init];
NSMutableDictionary *dTemp = [[NSMutableDictionary alloc] init];
STStockData *stockData = [[STStockData alloc] init];
for (int i = 0; i < [_arrTickers count]; i++) {
// get the ticker from its json form
dTemp = [_arrTickers objectAtIndex:i];
NSLog(#"Ticker: %#",[dTemp objectForKey:#"ticker"]);
// gets current data for ticker
[arrTemp addObjectsFromArray:[stockData getCurrentStockDataForTicker:[dTemp objectForKey:#"ticker"]]];
NSLog(#"Price %#",[arrTemp objectAtIndex:1]); // just to prove the array isnt nil.
// adds it to the dictionary
[_dStockData setObject:arrTemp forKey:[dTemp objectForKey:#"ticker"]];
NSLog(#"Dictionary %#",_dStockData);
// remove all objects so can reuse.
[arrTemp removeAllObjects];
dTemp = nil; // can't remove objects using [removeAllObjects] method. believe its due to it holding inside NSArrays which are immutable.
}
Here is the console output:
Initialize _dStockData
_dStockData = [[NSMutableDictionary alloc] init];
It is nil because use initialize stockData
STStockData *stockData = [[STStockData alloc] init];
and print _dStockData
NSLog(#"Dictionary %#",_dStockData);
I have a tableview that contains multiple sections, grouped by an attribute of my object (date) I try to sort the tableview according to the value of date.I created a function for that , but I get an error :
- (void)sortObjectsDictionnary:(NSArray *)arrayObjects
{
//this is my nsdictionnary
[objects removeAllObjects]
//this is nsmutableaaray that contains dats
[objectsIndex removeAllObjects];
NSMutableSet *keys = [[NSMutableSet alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy/MM/dd"];
for(int i=0;i<[arrayObjects count];i++){
Task *myTask=[arrayObjects objectAtIndex:i];
//curentsection contains my objects whith dates
NSMutableArray *currentSection = [objects objectForKey:taskDate];
if (currentSection == nil)
{
[keys addObject:taskDate];
currentSection = [[[NSMutableArray alloc] init] autorelease];
[objects setObject:currentSection forKey:taskDate];
}
// we add objet to the right section
[currentSection addObject:myTask];
}
[dateFormatter release];
for (id element in keys)
{
[objectsIndex addObject:element];
NSMutableArray *currentSection = [objects objectForKey:element];
//I get an error in this line
[currentSection sortUsingSelector:#selector(compare:)];
}
You haven't mentioned what the error message is and I am assuming the error message may be due to accessing NSMutableArray array object without initializing it. Try an alloc and init for array before
NSMutableArray *currentSection = [NSMutableArray]alloc]init];
currentSection = [objects objectForKey:element];
//I get an error in this line
[currentSection sortUsingSelector:#selector(compare:)];
Well it is purely a guess. Post your error message if this not works.
Bharath