How to write a NSString array object in one go? - objective-c

My problem is that I know NSString may used as an array and in my code I wrote it as such:
NSString *loadBackground[3];
loadBackground[0] = #"background1";
loadBackground[1] = #"background2";
loadBackground[2] = #"background3";
Is it possible to write the same code in one line instead of 4 lines?

Please use NSArray instead of c-arrays
NSArray *array = [NSArray arrayWithObjects:#"background1", #"background1" , #"background1", nil];
accessing a string:
NSString *aString = [array objectAtIndex:1]

Use an NSArray instead:
NSArray *bgs = [#"bg1,bg2,bg3" componentsSeparatedByString:#","]; // => [#"bg1", #"bg2", #"bg3"];

try this :
NSString *st11[3]={#"hai",#"hai1",#"hai2"};
NSLog(#"%#,%#,%#",st11[0],st11[1],st11[2]);

Not sure why you're not using NSArray, but if you really want to do it you can do:
NSString *loadBackground[3] = {#"background1", #"background2", #"background3"};

NSString *strs[3]={#"str1",#"str2",#"str3"};
use: strs[0];

Related

Adding ":" between two words in stringByAppendingString

I need to add : between 2 strings when i add them together.
I couldnt find simple way to do that,and i am sure there is .
NSString *inBase64;
inBase64=[userName.text stringByAppendingString:idNumber.text];
I need: userName:idNumber
Thnks
You can use stringWithFormat...
NSString *inBase64 = [NSString stringWithFormat:#"%#:%#", userName.text, idNumber.text];
Another way. Useful when you have a lot of string parts:
NSArray *parts = #[ userName.text, idNumber.text, /* more */ ];
NSString *string = [parts componentsJoinedByString:#":"];
instead of stringByAppendingString: use stringByAppendingFormat:, like this:
inBase64 = [userName.text stringByAppendingFormat:#":%#", idNumber.text];
you can also use the [NSString stringWithFormat:] method, like this:
inBase64 = [NSString stringWithFormat:#"%#:%#", userName.text, idNumber.text];

What does #[] stand for in objective c?

I've come across the #[] in the following context
self.searches = [#[] mutableCopy];
What is it?
self.searches = [[NSArray array] mutableCopy];
It's an array literal. In another words: [NSArray arrayWithObjects:nil] in short form.
You can add objects in it like this
NSArray *array = #[#"one", #"two"];

How to put elements from NSMutableArray into C char?

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

Objective-C string arrays

I have a string array as such:
NSArray *names;
names = [NSArray arrayWithObjects:
#"FirstList",
#"SecondList",
#"ThirdList",
nil];
I'm trying to assign an element of this string array to a string variable as such:
NSString *fileName = names[0]; // "Incompatible types in initialization"
or with casting
NSString *fileName = (NSString)names[0]; // "Conversion to non-scalar type requested"
I'm trying to do this, so I can use the string in a method that takes a string as an argument, such as:
NSString *plistPath = [bundle pathForResource:filetName ofType:#"plist"];
Is there no way to assign an element of a string array to a string variable?
Update from 2014: The code in this post actually would work these days since special syntactic support has been added to the framework and compiler for indexing NSArrays like names[0]. But at the time this question was asked, it gave the error mentioned in this question.
You don't use C array notation to access NSArray objects. Use the -objectAtIndex: method for your first example:
NSString *fileName = [names objectAtIndex:0];
The reason for this is that NSArray is not "part of Objective-C". It's just a class provided by Cocoa much like any that you could write, and doesn't get special syntax privileges.
NSArray is a specialized array class unlike C arrays. To reference its contents you send it an objectAtIndex: message:
NSString *fileName = [names objectAtIndex:0];
If you want to perform an explicit cast, you need to cast to an NSString * pointer, not an NSString:
NSString *fileName = (NSString *)[names objectAtIndex:0];
With the new Objective-C literals is possible to use:
NSString *fileName = names[0];
So your code could look like this:
- (void)test5518658
{
NSArray *names = #[
#"FirstList",
#"SecondList",
#"ThirdList"];
NSString *fileName = names[0];
XCTAssertEqual(#"FirstList", fileName, #"Names doesn't match ");
}
Check Object Subscripting for more information.

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.