I have looked at numerous posts which state various ways in which to remove an object from an array correctly, but I am not sure which method is best to use in my instance. I am loading a dictionary from a plist, this dictionary contains numerous arrays, and these arrays contain another dictionary. So I have 3 storage devices setup, 1 to hold the overall dictionary, another for an array, and finally a dictionary to hold the object from the array:
Header:
NSMutableDictionary *questionsDictionary;
NSMutableArray *questionsArray;
NSDictionary *currentQuestion;
#property (nonatomic, retain) NSMutableDictionary *questionsDictionary;
#property (nonatomic, retain) NSMutableArray *questionsArray;
#property (nonatomic, retain) NSDictionary *currentQuestion;
So my first question is to do with the above, are (nonatomic, retain) the right things to use for the following code.
Next I load in my dictionary from the plist:
.m:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:#"MultipleChoice.plist"];
self.questionsDictionary = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
I then setup my question array based upon type based upon my question type:
- (void)setupQuestionType : (NSString *)qType
{
if ([self.questionsDictionary objectForKey:qType])
{
self.questionsArray = [self.questionsDictionary objectForKey:qType];
[self pickRandomQuestion];
}
}
Finally (this is where I get the error), I want to grab the a question at random from this question category:
// Pick a random question number based upon amount of questions
int randomQuestionNum = [[NSNumber numberWithInt:(arc4random() % [self.questionsArray count])] intValue];
// Grab the dictionary entry for that question
currentQuestion = [self.questionsArray objectAtIndex:randomQuestionNum];
// Remove the question from the available questions
[self.questionsArray removeObjectAtIndex:randomQuestionNum]; (Error here)
// Set the question text
self.question.text = [currentQuestion objectForKey:kQuestionkey];
Now if I comment out the removeObjectAtIndex line then the code runs fine, and my question is displayed on the screen. This leads me to believe that it isn't a null pointer. So the logical answer points to the fact that self.questionsArray isn't a NSMutableArray. So I tried the following when setting the array:
- (void)setupQuestionType : (NSString *)qType
{
if ([self.questionsDictionary objectForKey:qType])
{
NSMutableArray *temp = (NSMutableArray *)[self.questionsDictionary objectForKey:qType];
self.questionsArray = (NSMutableArray *)temp;
[self pickRandomQuestion];
}
}
Purely to see if I could type_cast it but the error still occurs. Can anyone shed some light on what I'm doing wrong, or the best approach to take?
Don't typecast NSArray to NSMutableArray. Instead:
NSArray *temp = [self.questionsDictionary objectForKey:qType];
self.questionsArray = [NSMutableArray arrayWithArray:temp];
// code not tested.
Related
When I assign one array to another, I get an error, as shown in P1.
Then I checked the contents of _carIDArray and found an empty object in it. It was this object that caused the crash.P2
Why do empty objects appear in arrays? When using addobjcet: will not the array crash?
Here's some codes related to _carIDArray:
CarModel *car = [[CarModel alloc] initWithStatusAndMsgWithDic:object];
if (!_carIdArray) {
_carIdArray = [NSMutableArray array];
}
if (car.carIdStr && car.carIdStr.length>0){
[_carIdArray addObject:car.carIdStr];
}
*object is the data from the server.
#interface CarModel : NSObject
#property (nonatomic,copy) NSString *carIdStr;
#end
This question already has answers here:
Sort NSArray of custom objects based on sorting of another NSArray of strings
(5 answers)
Closed 7 years ago.
I have an NSArray of unique UUIDs sorted in the proper order. I also have another NSArray of bookmarks with a UUID encoding. I would like to sort my NSArray of bookmarks based on their UUID property into groups.
For example, I have an NSArray: #[#"uuid1", #"uuid3", #"uuid2"] that has been sorted into this particular order. Now the other NSArray must sort all of its bookmarks in the same order as the first NSArray above.
So the second NSArray is: #[#"bookmark1", #"bookmark2", #"bookmark3", ...etc.]
Say bookmark 1 has the UUID property encoded as uuid2, bookmark 2 has the UUID encoding of uuid 1, but the bookmark 3 has encoding of uuid3. How can I sort and group these bookmarks so that it would be: #[#"bookmark2", #"bookmark3", #"bookmark1"]?
Thanks!
You could get rid of the second array and use a dictionary instead, which is keyed on the UUID.
NSArray *sortedIDs = #[ #"uuid1", #"uuid3", #"uuid2", ];
NSDictionary *items = #{
#"uuid1" : #[ bookmark1 ],
#"uuid2" : #[ bookmark2 ],
#"uuid3" : #[ bookmark3 ],
};
Now when you want the second bookmark you can access it with
NSArray *bookmarksForUUID = items[sortedIDs[1]];
If you wanted to build the structure above you could add a category like the below to NSArray
- (NSDictionary *)pas_groupBy:(id (^)(id object))block;
{
NSParameterAssert(block);
NSMutableDictionary *groupedDictionary = NSMutableDictionary.new;
for (id object in self) {
id key = block(object);
if (groupedDictionary[key]) {
[groupedDictionary[key] addObject:object];
} else {
groupedDictionary[key] = [NSMutableArray arrayWithObject:object];
}
}
return groupedDictionary;
}
Then assuming your bookmark objects look something like
#interface Bookmark : NSObject
#property (nonatomic, copy) NSString *UUID;
// other properties
// other properties
#end
You can use the category like this
NSDictionary *bookmarksMappedBySection = ({
return [bookmarks pas_groupBy:^(Bookmark *bookmark) {
return bookmark.UUID;
};
});
Create a class Bookmark. Add both the UUIDs and title as properties to this class. Now you can have just one array which holds objects of class Bookmark and each one will have access to its UUID and title (as well as any other properties you might want to add later on).
#interface Bookmark: NSObject
#property (nonatomic,strong) NSString *uuid;
#property (nonatomic,strong) NSString *title;
#end
Now if you have NSArray (lets call it myBookmarks) you can do the following:
for (Bookmark *bookmark in myBookmarks) {
NSLog(#"my UUID is %#",bookmark.uuid);
NSLog(#"my title is %#",bookmark.title);
}
If you need to store these items in NSUserDefaults all you have to do is adopt the NSCoding protocol. If you don't know how to do it here is a great and simple example.
I declare the following in ViewController.h
#property (nonatomic, strong) NSMutableArray *locations;
and the following in ViewController.m
#implementation GHViewController
#synthesize locations;
...
for (FSFourSquareVenueObject *object in array) {
locations = [[NSMutableArray alloc] init];
[locations addObject:object.locationName];
NSLog(#"%#", locations);
}
This successfully logs all the string locations that have been placed in the locations NSMutableArray. How can I access this NSMutableArray in a different class?
I am trying to access it in my TableViewController class in order to display all the elements in the array. I have tried importing the ViewController.h file into my TableViewController.h file, yet I still cannot access the array from the ViewController class.
Remove the line
locations = [[NSMutableArray alloc] init];
from your for loop and place it somewhere like viewDidLoad or init. You're wiping out your array every time before adding a new object.
To access a single object across classes, you want to look into creating a singleton. There are many tutorials online.
Do as #Stonz2 suggests, but also modify your headers as follows:
In GHViewController.h:
#property (nonatomic, strong) NSArray *locations;
In GHViewController.m
#implementation GHViewController
#synthesize locations;
...
NSMutableArray *array = [[NSMutableArray alloc] init];
for (FSFourSquareVenueObject *object in array) {
[array addObject:object.locationName];
NSLog(#"%#", array);
}
self.locations = [array copy];
You can then access the array from another class using GHViewController -locations. You can edit the locations using the following snippet (or by creating a similar method in GHViewController):
NSMutableArray *array = [gh_viewController.locations mutableCopy];
[array addObject: newLocation];
gh_viewController.locations = [array copy];
Exposing a mutable array allows other class to modify the array without notifying GHViewController and vice versa. This can lead to unpredictable and hard to debug problems, such as if GHViewController removes some elements while TableViewController is iterating through all the objects. Using a non-mutable array prevents these sorts of bugs and ensures everyone has a consistent view of what's inside.
I have two classes - BNRItem and BNRContainer. BNRContainer is a subclass of BNRItem. In order to cut down the amount of code I paste, assume the following that I have tested and know works:
+(BNRItem * ) randomItem; // allocate and init a random item.
#property(nonatomic, readwrite, copy) NSMutableArray * subitems; // This is a property of BNRContainer class
main.m:
NSMutableArray * rand_items = [NSMutableArray alloc] init];
for (int i = 0; i < 10; i++) {
[rand_items addObject: [BNRItem randomItem]];
}
[rand_items addObject: #"HELLO"];
BNRContainer * rand_container_of_items = [BNRContainer randomItem];
rand_container_of_items.subitems = rand_items;
[rand_container_of_items.subitems addObject: #"THERE"]; // ERROR SIGABRT
NSLog(#"------------------------------------------------------");
NSLog(#"%#", rand_container_of_items);
rand_container_of_items = nil;
If I NSLog without adding #"THERE", I see "HELLO" in my description so I know that I am able to call addObject: at that point. Why do I get SIGABRT when I am trying to access ivar "subitems" of rand_container_of_items? I just can't figure this one out.
The problem seems to be the copy modifier in your declaration.
#property (nonatomic, readwrite, copy) NSMutableArray *subitems;
In the documentation, the NSCopying protocol conformance is inherited form NSArray, so my suspicion is that in this line
rand_container_of_items.subitems = rand_items;
subitems contains an immutable copy of the original array. Try removing copy from your declaration. If you need a copy use the mutableCopy method.
Problem lies here
property(nonatomic, readwrite, copy) NSMutableArray * subitems;
You should not use copy here since it will return immutable copy of the object. So that you cannot add objects to it. It could be
property(nonatomic, strong) NSMutableArray * subitems;
This line is giving sigbart as when you allocate an array to mutable array, it becomes mutable.
So, when you are copying rand_items to rand_container_of_items.subitem, it is becoming mutable.
So, to make it immutable, try following :
BNRContainer * rand_container_of_items = [BNRContainer randomItem];
rand_container_of_items.subitems = [rand_items mutablecopy];
[rand_container_of_items.subitems addObject:#"THERE"]; // ERROR SIGABRT
This question already has answers here:
How to remove all objects with the same property value but one in NSMutableArray
(3 answers)
Closed 9 years ago.
I have one NSObject with properties as following
#property (nonatomic, retain) NSString * destinationid;
#property (nonatomic, retain) NSString * destinationname;
#property (nonatomic, retain) NSString * assetid;
#property (nonatomic, retain) NSString * assetname;
#property (nonatomic, retain) NSString * assetdescription;
Here, I save this in NSMutableArray.
The data which I get from server contains same DestinationName, but different other properties.
I want to check, if same name of Object is already added to NSMutableArray, don't add it again.
I tried to user innner loops but no use :(
Thanks
Here's one solution:
NSArray *originalArray = ... // original array of objects with duplicates
NSMutableArray *uniqueArray = [NSMutableArray array];
NSMutableSet *names = [NSMutableSet set];
for (id obj in originalArray) {
NSString *destinationName = [obj destinationname];
if (![names containsObject:destinationName]) {
[uniqueArray addObject:obj];
[names addObject:destinationName];
}
}
One way to do it is to create a new array that only contains the destination names from your other array, and check if the name from your server is contained there:
NSArray *names = [self.mut valueForKey:#"destinationname"];
if (![names containsObject:destName]) {
[self.mut addObject:newObjectFromServer];
}
mut is the name of my mutable array, and destName is the destination name in the object your getting from the server.
This function should do the job:
BOOL contains(id object, NSMutableArray *array)
{
for (id array in yourArray)
if ([object.destinationname isEqualToString:yourObject.destinationname])
return YES;
return NO;
}
Usage example:
if (!contains(object, array))
[array addObject:object];