NSSet with string property from array of objects - ios

I have an array of somethings, I'd like to make a set from an NSString property on this object:
#protocol something<NSObject>
#property(nonatomic, readonly) NSString *Id;
#end
I have an array of somethings:
NSArray<id<something>> *arrayOfSomethings;
I'd like to get a NSSet of the Id properties:
NSSet<NSString *> *idSet = ?; // Calculate from arrayOfSomethings.
How do I do this? Thanks

You can use valueForKey: to create an array that contains your ids and then use that to create an NSSet
NSSet<NSString *> *idSet = [NSSet setWithArray:[arrayOfSomethings valueForKey:#"id"]];

Related

objective c No visible #interface for 'NSArray<NSString *>' declares the selector 'insertObject:atIndex:'

i want add object date in array self.datesWithEvent
#property (strong, nonatomic) NSArray<NSString *> *datesWithEvent;
[self.datesWithEvent addObject:date];
You can add / remove elements only to / from mutable objects
#property (strong, nonatomic) NSMutableArray<NSString *> *datesWithEvent;
However it's not sufficient to declare the property mutable, you have to initialize it with
[[NSMutableArray alloc] init];
Or if you assign another (immutable) array to that property you have to call mutableCopy
datesWithEvent = [someArray mutableCopy];
Only mutable object/instance of class allows to modify its internal information/elements.
Please read Object Mutability & NSMutableArray, documents provided by Apple. It will help you to understand why mutable class object is required here.
You should/must use NSMutableArray array to add/insert object into array.
#property (strong, nonatomic) NSMutableArray<NSString *> *datesWithEvent;
NSMutableArray - addObject
Inserts a given object at the end of the array.
Declaration
- (void)addObject:(ObjectType)anObject;
Solution to your query:
Note: Your array is NSString type of elements, so your date object must be NSString.
(Try this and let me know, what problem do you face. Also share your fullcode, with information about date object. )
#property (strong, nonatomic) NSMutableArray<NSString *> *datesWithEvent;
datesWithEvent = [[NSMutableArray alloc] init];
NSString *date = #"11-Dec-17";
[self.datesWithEvent addObject:date];

Create NSArray with specific property

I have an NSArray called carparkArr that contain MyCarpark objects. This class contain:
#property (nonatomic, assign) CLLocationCoordinate locationCoord;
Now I want to create an array that will have NSValue of each of these objects. How to do it quickly? I want something like:
NSArray *carparkArr = // fetching data
NSArray *locationProperty = [self getArrayForPropertyPath:#".locationCoord" forArray:carparkArr];
Yeah that feature already exists:
NSArray *locationProperty = [carparkArr valueForKey:#"locationCoord"];

Find objects which contains subjects with given parameter

I'm trying to figure out NSPredicate which will find all objects which contains subjects with given parameter.
I have this objects:
#interface User : NSManagedObject
#property (nonatomic, strong) NSString * name;
#property (nonatomic, strong) NSMutableOrderedSet * items;
#end
and:
#interface Item : NSManagedObject
#property (nonatomic, strong) NSNumber * itemId;
#end
I have array of users and I want to find all users which contain item with itemId == 1.
Have no more clues how to get it
NSNumber *theItemId = #1; // The id that you are looking for
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"ANY items.itemId == %#", theItemId];
should do it. You can use this predicate in a fetch request to fetch all matching users,
or with filteredArrayUsingPredicate: to filter an array of users.

Remove Duplicate Objects From NSMutableArray object property? [duplicate]

This question already has answers here:
How to remove all objects with the same property value but one in NSMutableArray
(3 answers)
Closed 9 years ago.
I have one NSObject with properties as following
#property (nonatomic, retain) NSString * destinationid;
#property (nonatomic, retain) NSString * destinationname;
#property (nonatomic, retain) NSString * assetid;
#property (nonatomic, retain) NSString * assetname;
#property (nonatomic, retain) NSString * assetdescription;
Here, I save this in NSMutableArray.
The data which I get from server contains same DestinationName, but different other properties.
I want to check, if same name of Object is already added to NSMutableArray, don't add it again.
I tried to user innner loops but no use :(
Thanks
Here's one solution:
NSArray *originalArray = ... // original array of objects with duplicates
NSMutableArray *uniqueArray = [NSMutableArray array];
NSMutableSet *names = [NSMutableSet set];
for (id obj in originalArray) {
NSString *destinationName = [obj destinationname];
if (![names containsObject:destinationName]) {
[uniqueArray addObject:obj];
[names addObject:destinationName];
}
}
One way to do it is to create a new array that only contains the destination names from your other array, and check if the name from your server is contained there:
NSArray *names = [self.mut valueForKey:#"destinationname"];
if (![names containsObject:destName]) {
[self.mut addObject:newObjectFromServer];
}
mut is the name of my mutable array, and destName is the destination name in the object your getting from the server.
This function should do the job:
BOOL contains(id object, NSMutableArray *array)
{
for (id array in yourArray)
if ([object.destinationname isEqualToString:yourObject.destinationname])
return YES;
return NO;
}
Usage example:
if (!contains(object, array))
[array addObject:object];

Subtract an array of complex objects from another array of same kind of objects ios

I know that subtracting an NSArray from NSArray if it is a single basic object found here
But what i have is an object like this
#interface Set : NSObject
#property (nonatomic, strong) NSString *ItemId;
#property (nonatomic, strong) NSString *time;
#property (nonatomic, strong) NSString *Category_id;
#property (nonatomic, strong) NSString *List_id;
#property (nonatomic, strong) NSString *name;
#end
How can i delete an array having the set object from another array with the same?
It can be done by iterations that i knw.Is there some other way ?
EDIT: For clarity
I have Array A with 5 Set objects and I have 4 Set Objects in Array B
array A and Array B contain 3 set objects with common values..[Note : memory may be different] common
All I need is an Array C =Array A - Array B that has 2 objects in resulting array C
Thank You :)
You need to implement the - (NSUInteger)hash and - (BOOL)isEqual:(id)object method in Set class.
For eg:-
- (NSUInteger)hash {
return [self.ItemId hash];
}
- (BOOL)isEqual:(id)object
{
return ([object isKindOfClass:[self class]] &&
[[object ItemId] isEqual:_ItemId])
}
After that try this:
NSMutableSet *set1 = [NSMutableSet setWithArray:array1];
NSMutableSet *set2 = [NSMutableSet setWithArray:array2];
[set1 intersectSet:set2]; //this will give you only the obejcts that are in both sets
NSArray *commonItems = [set1 allObjects];
[mutableArray1 removeObjectsInArray:commonItems];//mutableArray1 is the mutable copy of array1
mutableArray1 will have all objects in the same order as earlier after removing common objects.
By using NSSet and NSPredicate we can meet your requirement.
Assessors *ass1 = [[Assessors alloc] init];
ass1.AssessorID = #"3";
Assessors *ass2 = [[Assessors alloc] init];
ass2.AssessorID = #"2";
Assessors *ass3 = [[Assessors alloc] init];
ass3.AssessorID = #"1";
Assessors *ass4 = [[Assessors alloc] init];
ass4.AssessorID = #"2";
NSSet *nsset1 = [NSSet setWithObjects:ass1, ass2, nil];
NSSet *nsset2 = [NSSet setWithObjects:ass3, ass4, nil];
// retrieve the IDs of the objects in nsset2
NSSet *nsset2_ids = [nsset2 valueForKey:#"AssessorID"];
// only keep the objects of nsset1 whose 'id' are not in nsset2_ids
NSSet *nsset1_minus_nsset2 = [nsset1 filteredSetUsingPredicate:[NSPredicate predicateWithFormat:#"NOT AssessorID IN %#",nsset2_ids]];
for(Assessors *a in nsset1_minus_nsset2)
NSLog(#"Unique ID : %#",a.AssessorID);
Here Assessors is my NSObject Class (Set in your case) and AssessorID is one property of that class.
Hope this can help.

Resources