Using Swift with Cocoa and Objective-C
itunes.apple.com
itunes.apple.com
Also seems you can't override existing methods or properties still (actually achievable with swizzling of methods & property getter/setters on init but always seems way to heavy to use in reality). I would love to have a more mix-in friendly environment at some point in time.
“You can use extensions to add properties (including class and static properties). However, these properties must be computed; extensions can’t add stored properties to classes, structures, or enumerations.”
“You cannot use extensions to override existing methods or properties on Objective-C types.”
Excerpt From: Apple Inc. “Using Swift with Cocoa and Objective-C.” iBooks.
In particular, I found the required casting of AnyObject! to whatever I was expecting it to be not perfect (sometimes I thought that the typechecker really ought to figure some types out). Also, best practices for when to use NSArray vs. Array / NSString vs. String, etc. is another thing I look forward to.
Also, Some of the issues I ran into are probably due to Swift being beta, so knowing what the right way should be (even though it doesn't work that way yet) is important as it keeps me from hanging on to bad style.
Same issues I was having. I was porting an obj-c class to Swift and continued to use obj-c objects (NSString etc.). I then had to put 'as NSString' at the end of most lines. I'm considering going back and experimenting to see where I can use swift native objects. Hopefully this book will shed more light on the subject.
The code contained a type error that the XCode compiler either missed or crashed before reporting. The command line compiler detected and reported my error.
I guess not telling us the "right" way enables us to fuzz test the compiler. :)
UIColor *color = [UIColor colorWithRed:0.5 green:0.0 blue:0.5 alpha:1.0];
In Swift, you call it like this: let color = UIColor(red: 0.5, green: 0.0, blue: 0.5, alpha: 1.0)
I'm still unclear on how this is happening behind the scenes?The auto-translation process for initialisers is described immediately before the part you quote.
The `colorWith` factory method is useless in this context because a matching `initWith` initialiser exists. If the factory method were pattern matched in the same way, it would break things because it would produce a constructor with the same method signature as the translated initialiser.
1. Return type is `instancetype` or `MyClassName *`
2. Method takes at least one argument
3. Method name starts with a "class suffix"; that is, a suffix of the class name, with the restriction that you can't have partial words. The first letter may optionally be lower case.
The class suffix (optionally followed by "With" like in the `initWith` conversion) is stripped off and the rest of the method name is used for the first parameter, with the first letter lower-cased.
For example, the following conversions apply:
+[MyClassName myClassNameWithObject:obj] -> MyClassName(object: obj)
+[MyClassName classNameWithObject:obj] -> MyClassName(object: obj)
+[MyClassName nameObject:obj] -> MyClassName(object: obj);
However, since those all map to the same swift initializer, only one will be available (generally the first one declared)Here's a full English link: https://itunes.apple.com/us/book/using-swift-cocoa-objective...