NSArray from NSSet – Do I have to sort it myself?

You can specify a sort when you retrieve data with an NSFetchRequest by setting the sortDescriptors property to an array of NSSortDescriptors. But if you already have it in an NSSet and don’t want to make another fetch request, you can use: [[theSet allObjects] sortedArrayUsingDescriptors:sortDescriptors]; It’ll create an interim NSArray when you call allObjects and … Read more

Objective-C literals for NSSet and NSOrderedSet?

There is no Objective-C literal syntax for NSSet. It doesn’t really make sense to use index subscripting to access NSSet elements, although key-based subscripting might. Either by wrapping or subclassing, you could add your own. (Correction): Originally I had thought NSOrderedSet wasn’t supported either, but it turns out that using index subscripting is supported, but … Read more

Why doesn’t NSOrderedSet inherit from NSSet?

I went through the interface of NSSet and you’re right, ordered sets appear to satisfy the Liskov substitution principle and could therefor inherit from NSSet. There is one little method that breaks this: mutableCopy. The return value of mutableCopy must be an NSMutableSet, but NSMutableOrderedSet should inherit from NSOrderedSet. You can’t have both. Let me … Read more

How to search an NSSet or NSArray for an object which has an specific value for an specific property?

NSPredicate *predicate = [NSPredicate predicateWithFormat:@”type == %@”, @”standard”]; NSArray *filteredArray = [myArray filteredArrayUsingPredicate:predicate]; id firstFoundObject = nil; firstFoundObject = filteredArray.count > 0 ? filteredArray.firstObject : nil; NB: The notion of the first found object in an NSSet makes no sense since the order of the objects in a set is undefined.

How to create array of unique object list in Swift

As of Swift 1.2 (Xcode 6.3 beta), Swift has a native set type. From the release notes: A new Set data structure is included which provides a generic collection of unique elements, with full value semantics. It bridges with NSSet, providing functionality analogous to Array and Dictionary. Here are some simple usage examples: // Create … Read more

Creating an array from properties of objects in another array

This will return an array containing the value of licensePlate from each item in the myCars array: NSArray *licensePlates = [myCars valueForKeyPath:@”licensePlate”] If you want only unique items (for example), you can do something like this: NSArray *licensePlates = [myCars valueForKeyPath:@”@distinctUnionOfObjects.licensePlate”]; For more possibilities, see the Collection Operators documentation in the Key-Value Coding Programming Guide.

Getting an object from an NSSet

There are several use cases for a set. You could enumerate through (e.g. with enumerateObjectsUsingBlock or NSFastEnumeration), call containsObject to test for membership, use anyObject to get a member (not random), or convert it to an array (in no particular order) with allObjects. A set is appropriate when you don’t want duplicates, don’t care about … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)