nsarray

How to group by the elements of an array in Swift

懵懂的女人 提交于 2019-11-26 03:22:10
问题 Let\'s say that I have this code: class Stat { var statEvents : [StatEvents] = [] } struct StatEvents { var name: String var date: String var hours: Int } var currentStat = Stat() currentStat.statEvents = [ StatEvents(name: \"lunch\", date: \"01-01-2015\", hours: 1), StatEvents(name: \"dinner\", date: \"01-01-2015\", hours: 1), StatEvents(name: \"dinner\", date: \"01-01-2015\", hours: 1), StatEvents(name: \"lunch\", date: \"01-01-2015\", hours: 1), StatEvents(name: \"dinner\", date: \"01-01

Using NSPredicate to filter an NSArray based on NSDictionary keys

半城伤御伤魂 提交于 2019-11-26 03:05:01
问题 I have an array of dictionaries. I want to filter the array based on a key. I tried this: NSPredicate *predicate = [NSPredicate predicateWithFormat:@\"(SPORT == %@)\", @\"Football\"]; NSArray *filteredArray = [data filteredArrayUsingPredicate:predicate]; This doesn\'t work, I get no results. I think I\'m doing something wrong. I know this is the method if \"SPORT\" was an ivar. I think it is probably different if it is a key. I haven\'t been able to find an example however. Thanks Update I

How do I iterate over an NSArray?

ε祈祈猫儿з 提交于 2019-11-26 02:16:27
问题 I\'m looking for the standard idiom to iterate over an NSArray. My code needs to be suitable for OS X 10.4+. 回答1: The generally-preferred code for 10.5+/iOS. for (id object in array) { // do something with object } This construct is used to enumerate objects in a collection which conforms to the NSFastEnumeration protocol. This approach has a speed advantage because it stores pointers to several objects (obtained via a single method call) in a buffer and iterates through them by advancing

Best practice? - Array/Dictionary as a Core Data Entity Attribute [closed]

我们两清 提交于 2019-11-26 02:15:56
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 4 years ago . I am new to Core Data. I have noticed that collection types are not available as attribute types and would like to know what the most efficient way is of storing array/dictionary type data as an attribute (e.g. the elements that make up an address like street, city, etc. does

How to count occurrences of an element in a Swift array?

☆樱花仙子☆ 提交于 2019-11-26 02:08:33
问题 I\'ve seen a few examples of this but all of those seem to rely on knowing which element you want to count the occurrences of. My array is generated dynamically so I have no way of knowing which element I want to count the occurrences of (I want to count the occurrences of all of them). Can anyone advise? Thanks in advance EDIT: Perhaps I should have been clearer, the array will contain multiple different strings (e.g. [\"FOO\", \"FOO\", \"BAR\", \"FOOBAR\"] How can I count the occurrences of

Sort NSArray of date strings or objects

吃可爱长大的小学妹 提交于 2019-11-26 01:37:28
问题 I have an NSArray that contains date strings (i.e. NSString) like this: \"Thu, 21 May 09 19:10:09 -0700\" I need to sort the NSArray by date. I thought about converting the date string to an NSDate object first, but got stuck there on how to sort by the NSDate object. Thanks. 回答1: Store the dates as NSDate objects in an NS(Mutable)Array, then use -[NSArray sortedArrayUsingSelector: or -[NSMutableArray sortUsingSelector:] and pass @selector(compare:) as the parameter. The -[NSDate compare:]