I've a commun data in my application, and in some view I've to update those value only inside this view.
So I've created a local variable inside this view, then I set the value of those variable equal to the global variable and finally I've updated those global variable. This is my code :
if (_isCitySelector){
_dataArray = [[NSMutableArray alloc] initWithArray:[[Commun sharedInstance] stateArray]];
_subDataArray = [[NSMutableDictionary alloc] initWithDictionary:[[Commun sharedInstance] cityDictionary]];
} else {
_dataArray = [[NSMutableArray alloc] initWithArray:[[Commun sharedInstance] categoriesArray]];
_subDataArray = [[NSMutableDictionary alloc] initWithDictionary:[[Commun sharedInstance] subCategoriesDictionary]];
}
if (_activateParentSelection){
for (PFObject *object in _dataArray) {
NSMutableArray *tempArray = (NSMutableArray *)[_subDataArray valueForKey:object.objectId];
if ([[tempArray objectAtIndex:0][#"titre"] isEqualToString: #"الكل"])
continue;
PFObject *tempObject = [PFObject objectWithClassName:[object parseClassName]];
tempObject[#"titre"] = #"الكل";
if (object[#"nbrAnnonce"]){
tempObject[#"categorie_id"] = object;
tempObject[#"nbrAnnonce"] = #0;
}else
tempObject[#"region_id"] = object;
[tempArray insertObject:tempObject atIndex:0];
[_subDataArray setObject:tempArray forKey:object.objectId];
}
}
This code work's fine, but the problem this will update the global variable also ? what's wrong in my code !!!
Update
I can't use copyWithZone because my data type is PFObject and parse.com object doesn't support this function
You should try this init method for the array:
NSArray *dataArray = [[NSMutableArray alloc] initWithArray:[[Commun sharedInstance] stateArray] copyItems:YES];
This will create a separate copy of your data.
Related
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];
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];
What I am trying to achieve is the following, store one value (keyword) under the key (account) in one dictionary and then store that dictionary under another dictionary with the key (list) and finally that dictionary under another dictionary under the key (allKeys).
- (void)addFilterForAccount:(NSString *)account forKeyword:(NSString *)keyword inList:(NSString *)list {
//Set properties for NSStrings
account = _accountName;
keyword = _textField2.text;
list = _listName;
//Clear the textField
_textField2.text = #"";
//Store this information into a dictionary/array
_keys = [[NSMutableDictionary alloc] init];
[_keys setObject:keyword forKey:account];
_keywords = [[NSMutableDictionary alloc] init];
[_keywords setObject:_keys forKey:list];
_filters = [[NSMutableDictionary alloc] init];
[_filters setValue:_keywords forKey:#"allKeys"];
//nomenclature would be:
//
//_filters[#"allKeys"][#"//listName\\"][#"//accountName\\"]
NSLog(#"%#", _filters);
}
I have the following code that should do what I want, but the problem at this time is that it doesn't allow for multiple keys. So if I want to store multiple keywords under one list, when I log out it should be like this:
2014-09-07 18:31:12.562 Filterfeed[4050:124143] {
allKeys = {
"Breaking News" = (
{
BreakingNews = romney
obama
bush
clinton;
}
);
};
}
But right now it is just one name, and it gets replaced each time I invoke this method.
What actually prints out:
2014-09-07 18:31:12.562 Filterfeed[4050:124143] {
allKeys = {
"Breaking News" = (
{
BreakingNews = romney;
}
);
};
}
You want this method to be:
- (void)addFilterForAccount:(NSString *)account forKeyword:(NSString *)keyword inList:(NSString *)list {
//Set properties for NSStrings
account = _accountName;
keyword = _textField2.text;
list = _listName;
//Clear the textField
_textField2.text = #"";
//Store this information into a dictionary/array
_keys = [[NSMutableDictionary alloc] init];
NSMutableArray *array = [NSUserDefualts standardUserDefaults] objectForKey: list];
[array addObject: keyword];
[[NSUserDefualts standardUserDefaults] setObject: array forKey: list];
[[NSUserDefualts standardUserDefaults] synchronize];
NSMutableArray *array2 = [NSUserDefualts standardUserDefaults] objectForKey: list];
[_keys setObject:array2 forKey:account];
_keywords = [[NSMutableDictionary alloc] init];
[_keywords setObject:_keys forKey:list];
_filters = [[NSMutableDictionary alloc] init];
[_filters setValue:_keywords forKey:#"allKeys"];
//nomenclature would be:
//
//_filters[#"allKeys"][#"//listName\\"][#"//accountName\\"]
NSLog(#"%#", _filters);
}
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 am using a helper class in my app, to access a database and return an array of 5 objects, which I then assign to a property in my view controller.
In my view controller I call it like so:
- (void)viewDidLoad
{
[super viewDidLoad];
DatabaseHelper *helper = [[DatabaseHelper alloc] init];
self.trailersArray = [helper retrieveTrailers];
// Set trailer for first round
self.trailer = [self.trailersArray objectAtIndex:0];
// Prepare audio player
[self prepareToPlay];
// Swoop film screen in
[self swoopFilmScreenInAndAddPlayButton];
// Fade title in
[self fadeInTitleScreen];
// Initialise button array and set buttons to hidden
self.buttonOutlets = [NSArray arrayWithObjects:self.button1, self.button2, self.button3, self.button4, nil];
[self hideButtons];
// Initialise rounds
self.round = [NSNumber numberWithInt:-1];
// Initialise score which will also update graphics
[self initialiseScore:self.round];
self.scoreInteger = 0;
// Start first round
self.round = [NSNumber numberWithInt:0];
NSLog([self.trailersArray description]);
// User will trigger playing with Play IBAction
// User will trigger stop playing with Stop Playing IBaction
}
My problem is that once viewDidLoad is finished, the helper object seemingly disappears, as do its objects, and my self.trailersArray ends up pointing at nothing.
How do I fix this? Have tried deep copying, and using a retain attribute on the property but not working.
I can't use a class method because it ruins my helper object database methods but I am intrigued as to how class methods get around this memory problem?
Thanks for any help.
Alan
EDIT: As requested, code for retrieveTrailers below:
-(NSArray *)retrieveTrailers
{
// Get list of mp3 files in bundle and put in array
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *dirContents = [fm contentsOfDirectoryAtPath:bundleRoot error:nil];
NSPredicate *fltr = [NSPredicate predicateWithFormat:#"self ENDSWITH '.mp3'"];
NSArray *onlyMP3s = [dirContents filteredArrayUsingPredicate:fltr];
NSMutableArray *arrayOfTrailersWithMP3s = [[NSMutableArray alloc] init];
// Query database for objects where (unique id) = (mp3 file title)
for(NSString *string in onlyMP3s)
{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:#"Trailer"];
NSString *stringWithNoFileExtension = [string stringByReplacingOccurrencesOfString:#".mp3" withString:#""];
request.predicate = [NSPredicate predicateWithFormat:#"unique = %#", stringWithNoFileExtension];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:#"title" ascending:YES];
request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *array = [[self managedObjectContext] executeFetchRequest:request error:nil];
Trailer *trailer = [array lastObject];
NSLog([NSString stringWithFormat:#"Trailer Available:%#", trailer.title]);
if(trailer)
{
[arrayOfTrailersWithMP3s addObject:trailer];
}
}
// Choose five of the trailers at random and return
NSMutableArray *fiveRandomSelectedTrailers = [[NSMutableArray alloc] init];
int numberOfAvailableTrailers = [arrayOfTrailersWithMP3s count];
for(int i=0;i<5;i++)
{
int rand = (arc4random() % (numberOfAvailableTrailers));
Trailer *trailer = [arrayOfTrailersWithMP3s objectAtIndex:rand];
NSLog([NSString stringWithFormat:#"Trailer Chosen:%#", trailer.title]);
[fiveRandomSelectedTrailers addObject:trailer];
[arrayOfTrailersWithMP3s removeObject:trailer];
numberOfAvailableTrailers --;
}
return fiveRandomSelectedTrailers;
}
If that really is you viewDidLoad code what you are doing is creating a local object of your helper class which is then out of scope once the function has completed.
If you have a retained property of the helper class, you don't need the declaration: try doing it this way
In your .h file have a line like this:
#property(retain, nonatomic) DatabaseHelper *helper;
In your .m file make sure you have:
#synthesize helper;
In your viewDidLoad:
self.helper = [[DatabaseHelper alloc] init];
self.trailersArray = [self.helper retrieveTrailers];
This way you are creating an object of your helper class and assigning it to property, instead of creating a local variable. And, as you can see, you can use the property object of your helper class when you want to send it messages.
I'm assuming you are using MRC instead of ARC.