What does the #[] symbol represent in objective-c? - ios

I'm learning objective-c, Interviewer asked me this question
What's does the #[] symbol mean in objective-c?

It is a NSArray class literal. You can create instance using this
NSArray *array = #[];
the diff way to do this
NSArray *array = [[NSArray alloc] init];
The result the same

It is an empty array since you can create your array like this:
NSArray* array = #[#"A", #"B"];

Related

What sense to copy empty array?

I were reading another programmer's code, so i found this:
#property(nonatomic, strong) NSArray *assets;
----
_assets = [#[] mutableCopy];
__block NSMutableArray *tmpAssets = [#[] mutableCopy];
Is it some kind of trick? Why does he used mutableCopy to immutable array assets ? Why doesn't he just create it like:
self.assets = [NSArray new];
__block NSMutableArray *tmpAssets = [NSMutableArray new];
?
Just a shorthand to get an empty mutable array.
Instead of [NSMutableArray alloc] init] it's slightly less code to write -
the new construct isn't really used by the majority of Objective-C programmers and was added as a convenience to newcomers from other languages.
Mind that #[] will create an (immutable) NSArray -
there is no Objective-C literal for NSMutableArray.

How do I sort NSArrays by objects inside the objects inside the NSArray? [duplicate]

This question already has an answer here:
Sorting an NSArray by a key-value relationship that is 2 levels deep
(1 answer)
Closed 8 years ago.
Think Inception. Say I have 2 objects A and B and each instance of A has a variable B and each B has an NSNumber variable.
I have an NSArray Array with instances of A inside it and I want to sort it based on the NSNumber variable within B. So the normal approach of going
NSArray *array = [[NSArray alloc] initWithObjects:a1,a2,a3,a4,a5,nil];
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: #"b" ascending: YES];
array = [array sortedArrayUsingDescriptors: [NSArray arrayWithObject:sortOrder]];
just isn't going to cut it. Any ideas?
The sorting should work fine with this approach as long as you use the proper key for comparison as follows:
NSArray *array = [[NSArray alloc] initWithObjects:a1,a2,a3,a4,a5,nil];
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: #"b.nsnumbervar" ascending: YES];
array = [array sortedArrayUsingDescriptors: [NSArray arrayWithObject:sortOrder]];

Join objects in sub-array into one array - Objective-c

Is there a one-liner to do the following
NSMutableArray *allpoints = [[NSMutableArray alloc] init];
for (NSMutableArray *arr in self.points)
[allpoints addObjectsFromArray:arr];
I have an array of arrays (self.points) and I am joining all of the objects in the subarrays into a single array.
NSArray *array1 = #[ #"a", #"b", #"c" ];
NSArray *array2 = #[ #"d", #"e", #"f" ];
NSArray *array3 = #[ array1, array2 ];
NSArray * flattenedArray = [array3 valueForKeyPath:#"#unionOfArrays.self"];
NSLog(#"flattenedArray: %#", flattenedArray);
Output:
flattenedArray: (
a,
b,
c,
d,
e,
f
)
There is not a way to add all objects in an array of arrays (e.g., every NSMutableArray in self.points to another array without iterating through.
However, you could add a category to NSArray to do exactly what you're doing now, and then call it with one line later.
If you are initializing the array and adding objects at the same time then there is an initializer for that.
NSMutableArray *allpoints = [[NSMutableArray alloc] initWithArray:self.points];
If you already have the mutable array defined and you want to just append objects to the end then you can use the following.
[allpoints addObjectsFromArray:self.points];
I don't think there is a way to do this.
NSMutableArray *allpoints = [[NSMutableArray alloc] initWithArray:self.points]
would give you an array of the arrays, but there is no single line solution. I'd suggest writing a category that will do this for you so you can easily reuse it.

Stick NSString[] into NSArray

Why can't I stick NSString[] into NSArray? I get the following error "Implicit conversion of an indirect pointer to an Objective-C pointer to 'id' is disallowed with ARC"
Here's the code:
NSString *s1, *s2;
NSString *cArray[]={s1, s2};
NSMutableArray *dataArray = [[NSMutableArray alloc] init];
[dataArray addObject:cArray];
You cannot do it, because the ownership of cArray cannot be transferred.
If it is a local variable, it would disappear as soon as its scope ends, leaving your mutable array with a dangling reference.
Even if it is a global, there would be a problem, because your NSMutableArray would not know how to release the C array properly.
Objective C wants to protect you from situations like that as much as possible, providing nice classes such NSArray to make your job easier:
NSMutableArray *dataArray = [NSMutableArray array];
NSString *s1 = #"hello", *s2 = #"world";
// You can choose another constructor as you see fit.
NSArray *cArray = #[s1, s2];
[dataArray addObject:cArray];
NSArray holds objects. A C array is not an object, so you can't put it in an NSArray. If you want to create an NSArray out of a C array, you can use the arrayWithObjects:count: constructor.

get distinct value from NSmutablearray [duplicate]

This question already has answers here:
The best way to remove duplicate values from NSMutableArray in Objective-C?
(14 answers)
Closed 9 years ago.
i have a nsmutablearray as is shown below. I would like to retrieve list value without redundancy. in this example i should retrive 10 20 30
NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:#"10",#"20",#"30",#"30",#"30",#"30",#"20", nil];
Transform the array into a set, and then back into an array.
NSSet *set = [NSSet setWithArray:array];
NSArray *a = [set allObjects];
You can also have this new array sorted:
NSArray *a2 = [a sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2];
}];
Since iOS 5 you can use -[NSOrderedSet orderedSetWithArray:] which preserves the order:
NSArray *a2 = [[NSOrderedSet orderedSetWithArray:array] array];
NSArray *withRedunecy = [[NSSet setWithArray: array] allObjects];
This will be the one way like you can create new NSArray which has no duplicate objects or you need to create a logic for getting unique objects like insert one by one with checking is it already present or not.
Try to use this on
NSArray *array = #[#"10",#"20",#"30",#"30",#"30",#"30",#"20"];
NSArray *newArray = [[NSSet setWithArray:array] allObjects];
NSLog(#"%#", newArray);
Output :
(
30,
20,
10
)
Only this line of code will work fine .
NSSet *mySet = [NSSet setWithArray:array];
now mySet will have unique elements.so create array with this set
NSArray *myarray = [mySet allObjects];

Resources