At the moment I'm creating an array as per below:
NSArray *data = #[#[#20, #40, #20, #60, #40, #140, #80],];
However I'd like to be able to create the same array from data already in an NSMutableArray.
I've tried this but the library I'm passing the array to does not like it.
NSArray *data = [NSArray arrayWithArray:self.altData];
("altData" is an NSMutableArray)
Any ideas?
An NSMutbaleArray is a subclass of an NSArray. So you can just do NSArray *immutable = [myMutableArray copy] or if you know that you're not going to change the mutable array after a point then you can do NSArray *immutable = mutableArray; maybe followed by mutableArray = nil. Your solution is also fine.
Related
i have an array and i get value with this codes:
NSString *status = [[Sweetresponse objectAtIndex:path.row] objectAtIndex:9];
i wanna change this value :
[[Sweetresponse objectAtIndex:path.row] replaceObjectAtIndex:9 withObject:#"liked"];
But its not working because this structure is array in array. how can i fix this problem ?
This is because the NSArray is immutable. You have to make it mutable.
NSMutableArray *mutableResponse = [Sweetresponse mutableCopy];
NSMutableArray *mutableResponseItems = [[mutableResponse objectAtIndex:path.row] mutableCopy];
// replace at the index
[mutableResponseItems replaceObjectAtIndex:9 withObject:#"liked"];
// create immutables and replace it with our new array
mutableResponse[path.row] = [mutableResponseItems copy];
// set `Sweetresponse` (assuming it is an NSArray)
Sweetresponse = [mutableResponse copy];
edit: I have no idea what Sweetresponse really is, here I'm assuming it's a NSArray
#trojanfoe has a point with parsing this response into custom objects would lead to cleaner code and also simpler object modification.
How to addObject to NSArray using this code? I got this error message when trying to do it.
NSArray *shoppingList = #[#"Eggs", #"Milk"];
NSString *flour = #"Flour";
[shoppingList addObject:flour];
shoppingList += #["Baking Powder"]
Error message
/Users/xxxxx/Documents/iOS/xxxxx/main.m:54:23: No visible #interface for 'NSArray' declares the selector 'addObject:'
addObject works on NSMutableArray, not on NSArray, which is immutable.
If you have control over the array that you create, make shoppingList NSMutableArray:
NSMutableArray *shoppingList = [#[#"Eggs", #"Milk"] mutableCopy];
[shoppingList addObject:flour]; // Works with NSMutableArray
Otherwise, use less efficient
shoppingList = [shoppingList arrayByAddingObject:flour]; // Makes a copy
You can't add objects into NSArray. Use NSMutableArray instead :)
Your array cant be changed because is defined as NSArray which is inmutable (you can't add or remove elements) Convert it to a NSMutableArray using this
NSMutableArray *mutableShoppingList = [NSMutableArray arrayWithArray:shoppingList];
Then you can do
[mutableShoppingList addObject:flour];
NSArray does not have addObject: method, for this you have to use
NSMutableArray. NSMutableArray is used to create dynamic array.
NSArray *shoppingList = #[#"Eggs", #"Milk"];
NSString *flour = #"Flour";
NSMutableArray *mutableShoppingList = [NSMutableArray arrayWithArray: shoppingList];
[mutableShoppingList addObject:flour];
Or
NSMutableArray *shoppingList = [NSMutableArray arrayWithObjects:#"Eggs", #"Milk",nil];
NSString *flour = #"Flour";
[shoppingList addObject:flour];
I will be putting a variety of things in this mutable array, but first I am just trying to make sure it works by putting in strings, and then pulling out the strings. Here is my code
str1=#"1";
str2=#"2";
str3=#"3";
NSMutableArray *testArray;
[testArray addObject:str1];
[testArray addObject:str2];
[testArray addObject:str3];
retrieve =[testArray objectAtIndex:1];
NSLog(#"the test number is %#",retrieve);
The problem is that my string:retrieve equals "null" after receiving the string from the array. I'm not sure what I am doing wrong, I've looked at Apple's documentation but I'm having trouble making sense of it. I know I must be interacting with the array incorrectly, but I'm not sure how exactly. Help will be appreciated.
-Thank you!
You did not initialize your testArray:
NSMutableArray *testArray = [NSMutableArray array];
You can populate the array using new syntax. If you needed mutability only to add the three items, you could use a non-mutable array instead, like this:
NSArray *testArray = #[ #"1", #"2", #"3"];
If you do need mutability, call mutableCopy:
NSMutableArray *testArray = [#[ #"1", #"2", #"3"] mutableCopy];
Well, that's because testArray is nil. you should change the 4th line to
NSMutableArray *testArray = [NSMutableArray array];
You array is nil.
You are missing
NSMutableArray *testArray = [NSMutableArray array];
To clarify what others are telling you:
This line
NSMutableArray *testArray;
Does not create an array. It creates a pointer variable that can be used to point to a mutable array. It starts out containing a zero value (nil, points to nothing.)
It's like a postal address that points to an empty lot.
You need to create (allocate) and initialize a mutable array object in order to use it. (Continuing our analogy, you have to build a house and put a mailbox in front of it before the address becomes valid.)
So you need to say:
testArray = [#[ #"1", #"2", #"3"] mutableCopy];
Breaking that down:
The inner part,
#[ #"1", #"2", #"3"]
Creates an immutable array that contains 3 string objects.
Then we ask the immutable array to create a mutable copy of itself. We save the address of that newly created mutable array into the pointer variable testArray.
We could do it in 3 steps:
NSMutableArray *testArray;
NSArray *tempArray = #[ #"1", #"2", #"3"];
testArray = [tempArray mutableCopy];
Or all at once, like #dasblinkenlight's code:
NSMutableArray *testArray = [#[ #"1", #"2", #"3"] mutableCopy];
I made a small code
NSArray* _options = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:[UIImage imageNamed:#"2"],#"img",name,#"text"
, nil],nil];
Now, I want add other object to _options. What should i do?
I make more test but no success.
Thank for all
you can use [NSArray arrayByAddingObject:]
_options = [_options arrayByAddingObject:object];
or change _options to NSMutableArray
NSMutableArray *_options = [NSMutableArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:[UIImage imageNamed:#"2"],#"img",name,#"text"
, nil],nil];
[_options addObject:object];
and you may want to use modern syntax
NSMutableArray *_options = [#[#{#"img":[UIImage imageNamed:#"2"],#"text":name}] mutableCopy];
[_options addObject:object];
NSArray does not allow any changes to be made; you can use an NSMutableArray instead like this:
NSMutableArray *mutable = [_options mutableCopy];
[mutable addObject:yourObject];
NSDictionary is same in that it can't be mutated.
You can't add objects to a NSArray, to do so, you need a NSMutableArray.
However, you can add objects to NSArray when creating it with : arrayWithObjects
First, create an NSMutableArray, as you can make changes to it as you see fit later throughout your code:
NSMutableArray *newOptions = [NSMutableArray alloc]init];
[newOptions setArray:_options];
[newOptions addObject:yourObject];
I have an NSDictionary which contains several arrays with several strings in them. I want to put all strings under each array in one single array. How can I receive all the strings at once? I've tried this:
NSMutableArray *mutarr = [[NSMutableArray alloc] initWithObjects:[self.firstTableView allValues], nil];
NSArray *mySorted = [[NSArray alloc]init];
mySorted = [mutarr sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
NSLog(#"First table view values: %#", mySorted);
NOTE: self.firsttableview is a NSDictionary like this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"firstTableView" ofType:#"plist"];
self.firstTableView = [NSDictionary dictionaryWithContentsOfFile:path];
EFFECT: This gives me a list in NSLog, but it isn't in alphabetic order.
You are initializing your mutarr with one object which is the array returned by allValues on your dictionary. Instead try this:
NSMutableArray* mutarr = [NSMutableArray array];
for (NSArray* array in self.firstTableView.allValues) {
[mutarr addObjectsFromArray:array];
}
Your code is going to mean that mutarr is an array containing an array of arrays, not an array of strings. You should be looping over the array values from the dictionary and adding the items from each one to your mutarr to make it an array of strings that you can sort.
If your NSDictionary values are arrays then you are trying to sort the arrays instead of the strings. Try to create a for loop to iterate [self.firstTableView allValues] then add all values in that array to your main to sort array.