I have an NSMutableArray named randomSelection:
NSMutableArray *randomSelection;
I am then trying to add strings to this array if certain criteria are met:
[randomSelection addObject:#"string1"];
I am then trying to output the string, to determine if it has added it:
NSString *test = [randomSelection objectAtIndex:0];
NSLog(test);
However nothing is being output to the error log and I can't figure out why.
Any help/hints appreciated.
I think you are missing to allocate the memory for array. So try this
NSMutableArray *randomSelection = [[NSMutableArray alloc] init];
[randomSelection addObject:#"string1"];
NSString *test = [randomSelection objectAtIndex:0];
NSLog(test);
NSMutableArray *randomSelection = [[NSMutableArray alloc]init];
[randomSelection addObject:#"string1"];
You need to alloc it first.
First allocate the array using following statement & then objects in it.
NSMutableArray *randomSelection = [[NSMutableArray alloc] init];
[randomSelection addObject:[NSString stringWithFormat:#"String1"]];
[randomSelection addObject:[NSString stringWithFormat:#"String2"]];
NSLog(#"Array - %#", randomSelection);
This will definitely solves your problem.
Just allocate your NSMutableArray. You'll get solved your problem.
Try this:
NSMutableArray *randomSelection = [[NSMutableArray alloc]init];
[randomSelection addObject:#"string1"];
Swift :
var randomSelection: [AnyObject] = [AnyObject]()
randomSelection.append("string1")
let test: String = randomSelection[0] as! String
print(test)
OR
let array : NSMutableArray = []
array.addObject("test String")
print(array)
Related
NSArray has 3 values
#[#"addd:etyrhetwrwr", #"fdfdd:jjjjhhhh", #"fsuy:jjhwgggggg"]
And I want to put this array into the UILabel with word wrap.
So when I run, it should show like this in label.
addd:etyrhetwrwr
fdfdd:jjjjhhhh
fsuy:jjhwgggggg
But I can't separate this NSArray. How can I do that?
NSArray *myArray = #[#"addd:etyrhetwrwr", #"fdfdd:jjjjhhhh", #"fsuy:jjhwgggggg"];
NSString *labelString = [myArray componentsJoinedByString:#"\n"];
Then use the labelString to set your label text property.
Try This
NSArray *yourArray = #[#"addd:etyrhetwrwr", #"fdfdd:jjjjhhhh", #"fsuy:jjhwgggggg"];
NSString *string = [myArray componentsJoinedByString:#"\n"];
[yourLabel setText:string];
You can separate NSArray in UILabel by
NSArray myArr = #[#"addd:etyrhetwrwr", #"fdfdd:jjjjhhhh", #"fsuy:jjhwgggggg"];
NSString strLabel = [myArr string:#"\n"];
try this code
NSArray *array = [[NSArray alloc] initWithObjects:#"adad",#"fgfdgdfg",#"sddgfs", nil];
NSMutableString *strFinalData = [[NSMutableString alloc] init];
for (int i=0; i<[array count]; i++)
{
[strFinalData appendFormat:#"%# ",[array objectAtIndex:i]];
}
NSLog(#"Final String: %#",strFinalData);
Now set yourLabel.text = strFinalData;
Hi this question got asked before: How do you deep copy a multidimensional NSMutableArray and its contents?
but the answer didn't work for me.
I tried:
NSMutableArray *copy = [[NSMutableArray alloc] initWithArray:multiDimArray copyItems:YES];
and:
NSMutableArray *mutableArray = [multiDimArray mutableCopy];
NSMutableArray *copy = [[NSMutableArray alloc] initWithArray:mutableArray copyItems:YES];
The copying works but when I try to replace an object I get the error:
-[__NSArrayI replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance 0x1073b1790
Am I doing anything wrong or is it just not possible to deep copy a multidimensional array without its elements becoming immutable?
Is there a workaround for this?
Thanks
Try
NSMutableArray *mutableArray = [multiDimArray mutableCopy];
replace with
NSMutableArray *mutableArray = [[NSMutableArray arrayWithArray:multiDimArray] mutableCopy];
I got it to work with this:
NSMutableArray *originalArray = [NSMutableArray new];
NSMutableArray *copyArray = [NSMutableArray new];
for(NSArray *subarray in originalArray) {
NSMutableArray *subCopy = [NSMutableArray new];
for (id item in subarray){
[subCopy addObject:[item copy]];
}
[copyArray addObject: subCopy];
}
I did it for a two dimensional array. If there are more dimensions you probably have to add an extra for loop for every dimension.
I have an NSArray of custom objects that all have a #property name of type NSString. How can I quickly enumerate through the array and create a new array that contains only the objects that have a specific word in their name property?
For instance:
CustomObject *firstObject = [[CustomObject alloc] init];
firstObject.name = #"dog";
CustomObject *secondObject = [[CustomObject alloc] init];
secondObject.name = #"cat";
CustomObject *thirdObject = [[CustomObject alloc] init];
thirdObject.name = #"dogs are fun";
NSMutableArray *testArray = [NSMutableArray arrayWithObjects:firstObject,
secondObject,
thirdObject,
nil];
// I want to create a new array that contains all objects that have the word
// "dog" in their name property.
I know I could use a for loop like so:
NSMutableArray *newArray = [NSMutableArray array];
for (CustomObject *obj in testArray)
{
if ([obj.name rangeOfString:#"dog"].location == NSNotFound) {
//string wasn't found
}
else {
[newArray addObject:obj];
}
}
But is there a more efficient way? Thanks!
NSString *searchString = #"dog";
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF.name contains %#", searchString];
NSArray *filteredArray = [testArray filteredArrayUsingPredicate:predicate];
Please have a look at NSPredicates ! They are highly efficient when you are searching / filtering through array results. This is the documentation!
I have a NSMutableArray and I need to sort its elements into separate C char.
How to accomplish that? Or is it better to put the elements into NSStrings?
I've tried this, and it crashes when I try to log the result:
NSMutableArray *array = [[NSMutableArray alloc] init];
... do something to fill the array ...
NSString *string;
string = [array objectAtIndex:0];
NSLog(#"String: %#", string);
*I really prefer putting the elements of the array into C char, because I already have some woking code using char instead of NSStrin*g.
Thanks!
Dont see any specific reason to convert NSString to C chars. To sort an array full of NSStrings try this method -
NSMutableArray *array = [[NSMutableArray alloc] init];
sortedArray = [array sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
NSString *string = [sortedArray objectAtIndex:0];
NSLog(#"String: %#", string);
is it an possible to add a value from an NSMutableString into an NSArray? Whats the snippet?
Actually, Mike is wrong. If you want to instantiate an NSArray with a single NSMutableString object, you can do the following:
NSMutableString *myString; //Assuming your string is here
NSArray *array = [NSArray arrayWithObject:myString];
There is no arrayWithElements in NSArray (see NSArray documentation)
If you want to instantiate an NSArray with a single NSMutableString object, you can do the following:
NSString *myString; //Assuming your string is here
NSArray *array = [NSArray arrayWithObjects:myString,nil];
Note that NSArray will be immutable - that is, you can't add or remove objects to it after you've made it. If you want the array to be mutable, you'll have to create an NSMutableArray. To use an NSMutableArray in this fashion, you can do the following:
NSString *myString; //Assuming your string is here
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:myString];
NSArray is immutable, so you cannot add values to it. You should use NSMutableArray in order to do that with the addObject: method.
NSMutableString *str = ...
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:str];
// You must use NSMutableArray to add Object to array
NSMutableArray *tableCellNames;
// arrayWithCapacity is a required parameter to define limit of your object.
tableCellNames = [NSMutableArray arrayWithCapacity:total_rows];
[tableCellNames addObject:title];
NSLog(#"Array table cell %#",tableCellNames);
//Thanks VKJ
An elegant solution would be this:
NSMutableString *str; //your string here
NSArray *newArray = #[str];
Using the new notation, it's a piece of cake.