2

I wish to know how to convert an NSArray (for example: ) into an Objective-C string (NSString).

Also, how do I concatenate two strings together? So, in PHP it's:

$variable1 = "string one":
$variable2 = $variable1;

But I need it in Objective-C

ProgramFOX
  • 6,131
  • 11
  • 45
  • 51
user2979661
  • 23
  • 1
  • 3
  • 1
    Two questions, both answered in the NSString documentation. Which bit are you stuck on? – Abizern Nov 11 '13 at 15:35
  • Use [NSString stringByAppendingString:@"%@%@", str1, str2]; – Dave Nov 11 '13 at 15:37
  • Every `Objective-C` object has a default `description` method (inherited at least from `NSObject`), which returns some sort of string representation of the object as an `NSString`. For `NSArray`, it returns some details on the memory location of the array. If you're looking for something different, you'll need to clarify your question. Please note that an `NSArray` simply holds pointers to objects... any object... and an array holding an `NSString`, `NSNumber`, `NSDictionary`, and `UIButton`, for example, doesn't necessarily have a good string representation of its contents. – nhgrif Nov 11 '13 at 15:39

1 Answers1

2

Possible duplication: Convert NSArray to NSString in Objective-C

Firstly, that is not PHP concatenation, This is:

  $variable1 = "Hello":
  $variable1 .= "World";

see: https://stackoverflow.com/a/11441389/1255945

Next, Stackoverflow isnt a personal tutor. You should only post here specific problems and provide as much code and information as you can, not just stuff thats basically saying "I cant be bothered to look myself, tell me".

I must admit I have done this myself so i'm not having a go at you, just trying to be polite as share my knowledge and experience

With that in mind, to convert an NSArray to NSString

Taken from: http://ios-blog.co.uk/tutorials/objective-c-strings-a-guide-for-beginners/

  NSString * resultString = [[array valueForKey:@"description"] componentsJoinedByString:@""];

If you want to split the string into an array use a method called componentsSeparatedByString to achieve this:

  NSString *yourString = @"This is a test string";
  NSArray *yourWords = [myString componentsSeparatedByString:@" "];

  // yourWords is now: [@"This", @"is", @"a", @"test", @"string"]

if you need to split on a set of several different characters, use NSString’s componentsSeparatedByCharactersInSet:

  NSString *yourString = @"Foo-bar/iOS-Blog";
  NSArray *yourWords = [myString componentsSeparatedByCharactersInSet:
              [NSCharacterSet characterSetWithCharactersInString:@"-/"]
            ];

  // yourWords is now: [@"Foo", @"bar", @"iOS", @"Blog"]

Note however that the separator string can’t be blank. If you need to separate a string into its individual characters, just loop through the length of the string and convert each char into a new string:

  NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[myString length]];
    for (int i=0; i < [myString length]; i++) {
    NSString *ichar  = [NSString stringWithFormat:@"%c", [myString characterAtIndex:i]];
    [characters addObject:ichar];
}

Hope this helps, and Good luck developing :)

Community
  • 1
  • 1
MarkP
  • 2,546
  • 5
  • 31
  • 48
  • 1
    If you think a question is a duplicate, then it's a good idea to mark it as a duplicate. – Abizern Nov 11 '13 at 15:36
  • As a note, `NSString *rejoined = [[[@"This is a test string" componentsSeperatedByString:@" "] valueForKey:@"description"] componentsJoinedByString:@""];` results in `rejoined` containing the string `"Thisisateststring"`... – nhgrif Nov 11 '13 at 15:45
  • oh i've never marked a comment/question as a duplicate - How do you do that? – MarkP Nov 11 '13 at 15:46
  • 1
    Thanks... for the replies and help. Sorry didn't mean to be such a newbie – user2979661 Nov 11 '13 at 15:50