How to put elements from NSMutableArray into C char? - objective-c

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);

Related

NSString to NSArray and editing every object

I have an NSString filled with objects seperated by a comma
NSString *string = #"1,2,3,4";
I need to seperate those numbers and store then into an array while editing them, the result should be
element 0 = 0:1,
element 1 = 1:2,
element 2 = 2:3,
element 3 = 3:4.
How can i add those to my objects in the string ??
Thanks.
P.S : EDIT
I already did that :
NSString *string = #"1,2,3,4";
NSArray *array = [string componentsSeparatedByString:#","];
[array objectAtIndex:0];//1
[array objectAtIndex:1];//2
[array objectAtIndex:2];//3
[array objectAtIndex:3];//4
I need the result to be :
[array objectAtIndex:0];//0:1
[array objectAtIndex:1];//1:2
[array objectAtIndex:2];//2:3
[array objectAtIndex:3];//3:4
In lieu of a built in map function (yey for Swift) you would have to iterate over the array and construct a new array containing the desired strings:
NSString *string = #"1,2,3,4";
NSArray *array = [string componentsSeparatedByString:#","];
NSMutableArray *newArray = [NSMutableArray arrayWithCapacity:array.count];
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[newArray addObject:[NSString stringWithFormat:#"%lu:%#", (unsigned long)idx, obj]];
}];
The first thing you need to do is separate the string into an array of component parts - NSString has a handy method for that : '-componentsSeparatedByString'. Code should be something like this :
NSArray *components = [string componentsSeparatedByString:#","];
So that gives you 4 NSString objects in your array. You could then iterate through them to make compound objects in your array, though you arent exactly clear how or why you need those. Maybe something like this :
NSMutableArray *resultItems = [NSMutableArray array];
for (NSString *item in components)
{
NSString *newItem = [NSString stringWithFormat:#"%#: ... create your new item", item];
[resultItems addObject:newItem];
}
How about this?
NSString *string = #"1,2,3,4";
NSArray *myOldarray = [string componentsSeparatedByString:#","];
NSMutableArray *myNewArray = [[NSMutableArray alloc] init];
for (int i=0;i<myOldarray.count;i++) {
[myNewArray addObject:[NSString stringWithFormat:#"%#:%d", [myOldarray objectAtIndex:i], ([[myOldarray objectAtIndex:i] intValue]+1)]];
}
// now you have myNewArray what you want.
This is with consideration that in array you want number:number+1

How to add string objects to NSMutableArray

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)

Construct NSString from the description method of each NSArray item?

I have an NSArray, where each object contains a specific class called Card. Card has a description method. I want to join all objects in the array using the output of the description method, separated by spaces. Is there a simple to do this, without manually iterating the NSArray and manipulating NSString?
Something akin to the following made-up code?
NSArray *myArray = getCards(); // fetches 10 items or more
NSString *myString = [myArray joinUsingDescriptionMethodSeparatedBy:#" "];
or
NSString *myString = [NSString stringFromArrayDescriptionMethods:myArray separatedBy:#" "];
Naturally ,I could implement this myself but I suspect there could be something already present that does this.
I don't think that there is such a method. You can also implement it in a Category for NSString.
Sorry, I found this:
NSString * result = [[array valueForKey:#"description"] componentsJoinedByString:#""];
From the documentation:
Constructs and returns an NSString object that is the result of
interposing a given separator between the elements of the array.
- (NSString *)componentsJoinedByString:(NSString *)separator
Do this for description method of each NSArray item:
NSMutableString * result = [[NSMutableString alloc] init];
for (NSObject * obj in array)
{
[result appendString:[NSString stringWithFormat:#" %#"[obj description]]];
}
NSLog(#"The concatenated string is %#", result);

NSArray filled with bool

I am trying to create an NSArray of bool values. How many I do this please?
NSArray *array = [[NSArray alloc] init];
array[0] = YES;
this does not work for me.
Thanks
NSArrays are not c-arrays. You cant access the values of an NSArray with array[foo];
But you can use c type arrays inside objective-C without problems.
The Objective-C approach would be:
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[NSNumber numberWithBool:YES]];
//or
[array addObject:#(NO)];
...
BOOL b = [[array objectAtIndex:0] boolValue];
....
[array release];
EDIT: New versions of clang, the now standard compiler for objective-c, understand Object subscripting. When you use a new version of clang you will be able to use array[0] = #YES
Seems like you've confused c array with objc NSArray. NSArray is more like a list in Java, into which you can add objects, but not values like NSInteger, BOOL, double etc. If you wish to store such values in an NSArray, you first need to create a mutable array:
NSMutableArray* array = [[NSMutableArray alloc] init];
And then add proper object to it (in this case we'll use NSNumber to store your BOOL value):
[array addObject:[NSNumber numberWithBool:yourBoolValue]];
And that's pretty much it! If you wish to access the bool value, just call:
BOOL yourBoolValue = [[array objectAtIndex:0] boolValue];
Cheers,
Pawel
Use [NSNumber numberWithBool: YES] to get an object you can put in the collection.

How add data from NSMutableString into NSArray?

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.