7 ms·
Using Swift with Cocoa and Objective-C
- k-mcgrady 12y agoI'm getting 'this page is temporarily unavailable' on this new book and the first one in the iBooks app on Mac. Edit: UK store so maybe it's still propagating to all stores.
- wil421 12y agoThis page is in Italian.
- jackjeff 12y agoIt's from the Italian store. The book is in English though. Here's a full English link: https://itunes.apple.com/us/book/using-swift-cocoa-objective/id888894773?mt=11 https://itunes.apple.com/us/book/using-swift-cocoa-objective...
- parasubvert 12y agoEnglish book here (Canada link but should redirect you): https://itunes.apple.com/ca/book/using-swift-cocoa-objective/id888894773?mt=11 https://itunes.apple.com/ca/book/using-swift-cocoa-objective...
- Nib 12y agoWhat language is this in?
- jvreeland 12y agoI think, Italian? if you switch the language specified in the url from 'it' to 'en' you get English, and I imagine 'it' stands for Italian.
- napolux 12y agoEnglish
- thought_alarm 12y agoSwift.
- chrisdevereux 12y agoPDF: https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/buildingcocoaapps/BuildingCocoaApps.pdf https://developer.apple.com/library/prerelease/ios/documenta...
- seanalltogether 12y agoFor consistency and simplicity, Objective-C factory methods get mapped as convenience initializers in Swift. This mapping allows them to be used with the same concise, clear syntax as initializers. For example, whereas in Objective-C you would call this factory method like this: 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?
- timanglade 12y agoThey generate new Swift headers for Objective-C classes, and have translation rules in their toolset to remap/rename some arguments (all the init methods for example). The tool can even do that on your own Objective-C projects. I used this on a side project this weekend and it’s pretty rad.
- seanalltogether 12y agoAre these translation rules published somewhere? I'd really like to understand what keywords trigger what kind of mapping rules.
- JimDabell 12y agoI'm pretty sure that's just a misstatement in the documentation. The Swift code is equivalent to the UIColor initialiser, there's no need to auto-translate the factory method as well. The auto-translation process for initialisers is described immediately before the part you quote.
- seanalltogether 12y agoThe section above mentions specifically translating either "init" or "initWith" from initializers. I'm curious if they're also pattern matching the "colorWith" prefix or if its more complicated.
- terhechte 12y agoAwesome. I just finished a small Mac App in Swift [1], and I've run into lots of small situations where I wasn't sure what the best practice would be. Really looking forward to reading this one. 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. [1] https://github.com/terhechte/Swijito https://github.com/terhechte/Swijito
- k-mcgrady 12y ago>> "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." 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.
- dunham 12y agoYeah, I managed to crash the compiler (SEGV) with AnyObject! casting. (I got the wrong idiom for iterating through an AnyObject[] ) 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. :)
- alex_marchant 12y agoThis is the same as the online documentation that's been up since release: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/ https://developer.apple.com/library/prerelease/ios/documenta...
- dang 12y agoThanks. Burying as dupe.
- roskilli 12y agoWell this is rather upsetting, it seems extensions still cannot add stored properties... Back to using objc_setassociatedobject and objc_getassociatedobject it seems for extension properties, sadface. 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.
- nextstep 12y agoNot surprising. Class extensions in Objective-C cannot add stored properties either; objc_setassociatedobject and objc_getassociatedobject are shitty hacks.
- msie 12y agoI wish there was a properly formatted pdf version of the language guide. There is one floating around but the font size is too small.