Tag: enumeration

Objective-C / C giving enums default values

I read somewhere about giving enums default values like so: typedef enum { MarketNavigationTypeNone = 0, MarketNavigationTypeHeirachy = 1, MarketNavigationTypeMarket = 2 } MarketNavigationLevelType; .. but i can’t remember the value of doing this. If i don’t give them default values – and then someone later on reorders the enum – what are the risks? […]

Enumerate NSDictionary with keys and objects, PHP style

I know you can Enumerate the keys or values of NSMutableDictionary using NSEnumerator. Is it possible to do both together? I’m looking for something similar to the PHP foreach enumerator like: foreach ($dictionary as $key => $value);

Objective-C 2.0 and Fast Enumeration throwing exceptions

I have a block of code which is similar to the following: for (NSDictionary *tmp in aCollection) { if ([[bar valueForKey:@”id”] isEqualToString:[tmp valueForKey:@”id”]]) { break; } else { [aCollection addObject:bar]; } } Is this technically an exception in Objective-C 2.0? It appears you cannot mutate a collection with fast enumeration. This is the result of […]

How to count the number of lines in an Objective-C string (NSString)?

I want to count the lines in an NSString in Objective-C. NSInteger lineNum = 0; NSString *string = @”abcde\nfghijk\nlmnopq\nrstu”; NSInteger length = [string length]; NSRange range = NSMakeRange(0, length); while (range.location < length) { range = [string lineRangeForRange:NSMakeRange(range.location, 0)]; range.location = NSMaxRange(range); lineNum += 1; } Is there an easier way?

for each loop in objective c for accessing NSMutable dictionary

I am finding some difficulty in accessing mutable dictionary keys and values in Objective-C. Suppose I have this: NSMutableDictionary *xyz=[[NSMutableDictionary alloc] init]; I can set keys and values. Now, I just want to access each key and value, but I don’t know the number of keys set. In PHP it is very easy, something as […]

What is the BOOL *stop argument for enumerateObjectsUsingBlock: used for?

I’ve been using enumerateObjectsUsingBlock: a lot lately for my fast-enumeration needs, and I’m having a hard time understanding the usage of BOOL *stop in the enumeration block. The NSArray class reference states stop: A reference to a Boolean value. The block can set the value to YES to stop further processing of the array. The […]

How do I iterate over an NSArray?

I’m looking for the standard idiom to iterate over an NSArray. My code needs to be suitable for OS X 10.4+.

SWIFT ALAssetsLibrary not enumerating groups

I’m trying to gather thumbnails of all the user’s images into an array, but when I call the enumerateAssetsUsingBlock method of ALAssetsLibrary nothing seems to happen. import UIKit import AssetsLibrary class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { @IBOutlet var photoLibView: UICollectionView var assetLibrary : ALAssetsLibrary = ALAssetsLibrary() func showCustomLibrary() { self.assetLibrary = ALAssetsLibrary() var assetsArray : […]

how to count number of sprites swift

I’m building an app that add sprites to the screen. In several parts of my code I want to know how many sprites I have with a certain key. At the moment I implemented it in this way var counter = 0 enumerateChildNodesWithName(“box”) { node, _ in counter = counter + 1 } println(counter) Is […]

Iterating through an Enum in Swift 3.0

I have a simple enum that I would like to iterate over. For this purpose, I’ve adopted Sequence and IteratorProtocol as shown in the code below. BTW, this can be copy/pasted to a Playground in Xcode 8. import UIKit enum Sections: Int { case Section0 = 0 case Section1 case Section2 } extension Sections : […]