Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSString from NSArray

I am trying to create a String from Array.But, there is condition to how it should be generated, as explained below.

NSArray *array=[NSArray arrayWithObjects:@"Hello",@"World",nil];
[array componentsJoinedByString:@","];

This will output: Hello,World.

But, if first Item is Empty,then is there way to receive the only second one.

  1. Hello , @"" => Hello
  2. @"" , World => World
  3. Hello , World => Hello,World
like image 994
NNikN Avatar asked Jan 16 '13 19:01

NNikN


People also ask

What does Nsstring mean?

A static, plain-text Unicode string object which you use when you need reference semantics or other Foundation-specific behavior.

Can Nsarray contain nil?

arrays can't contain nil. There is a special object, NSNull ( [NSNull null] ), that serves as a placeholder for nil.

What is swift Nsarray?

An object representing a static ordered collection, for use instead of an Array constant in cases that require reference semantics.


1 Answers

Another way to do this is to grab a mutable copy of the array and just remove non valid objects. Something like this perhaps:

NSMutableArray *array = [[NSArray arrayWithObjects:@"",@"World",nil] mutableCopy];
[array removeObject:@""]; // Remove empty strings
[array removeObject:[NSNull null]]; // Or nulls maybe

NSLog(@"%@", [array componentsJoinedByString:@","]);
like image 50
Alladinian Avatar answered Oct 05 '22 14:10

Alladinian