News for hackers. Stuff that matters.
Re:Using Swift with Cocoa and Objective-C by roskilli on Friday June 13, 2014 @11:14AM (#7889218)
Well 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.

Re:Using Swift with Cocoa and Objective-C by nextstep on Friday June 13, 2014 @11:18AM (#7889237)
Not surprising. Class extensions in Objective-C cannot add stored properties either; objc_setassociatedobject and objc_getassociatedobject are shitty hacks.
Re:Using Swift with Cocoa and Objective-C by chrisdevereux on Friday June 13, 2014 @10:26AM (#7888990)
Re:Using Swift with Cocoa and Objective-C by alex_marchant on Friday June 13, 2014 @11:06AM (#7889176)
This is the same as the online documentation that's been up since release: https://developer.apple.com/library/prerelease/ios/documenta...
Re:Using Swift with Cocoa and Objective-C by dang on Friday June 13, 2014 @01:38PM (#7889951)
Thanks. Burying as dupe.
Re:Using Swift with Cocoa and Objective-C by terhechte on Friday June 13, 2014 @11:02AM (#7889161)
Awesome. 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

Re:Using Swift with Cocoa and Objective-C by k-mcgrady on Friday June 13, 2014 @11:10AM (#7889193)
>> "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.

Re:Using Swift with Cocoa and Objective-C by dunham on Friday June 13, 2014 @12:31PM (#7889639)
Yeah, 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. :)

Re:Using Swift with Cocoa and Objective-C by seanalltogether on Friday June 13, 2014 @10:47AM (#7889086)
For 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?
Re:Using Swift with Cocoa and Objective-C by timanglade on Friday June 13, 2014 @10:57AM (#7889134)
They 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.
Re:Using Swift with Cocoa and Objective-C by seanalltogether on Friday June 13, 2014 @11:02AM (#7889165)
Are these translation rules published somewhere? I'd really like to understand what keywords trigger what kind of mapping rules.
Re:Using Swift with Cocoa and Objective-C by JimDabell on Friday June 13, 2014 @11:12AM (#7889203)
I'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.

Re:Using Swift with Cocoa and Objective-C by seanalltogether on Friday June 13, 2014 @12:15PM (#7889560)
The 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.
Re:Using Swift with Cocoa and Objective-C by JimDabell on Friday June 13, 2014 @12:33PM (#7889653)
I think you might have missed my point: the `initWith` translation produces the Swift constructor that the documentation you quoted presents as the factory method equivalent.

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.

Re:Using Swift with Cocoa and Objective-C by jackhlaw on Friday June 13, 2014 @12:58PM (#7889765)
We actually _do_ pattern match UIColor colorWith and turn it into an initializer. If there are two initializers after we pattern match (e.g., UIColor colorWith and initWith), we eliminate the duplicate.
Re:Using Swift with Cocoa and Objective-C by seanalltogether on Friday June 13, 2014 @01:33PM (#7889930)
Are there other pattern matches that can get triggered? I've come across classes that initialize with "For" as in [MyClass classForString:str] or similar.
Re:Using Swift with Cocoa and Objective-C by jayrhynas on Friday June 13, 2014 @01:58PM (#7890065)
From what I've been able to figure out just by playing around, the following rules are used:

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)
Re:Using Swift with Cocoa and Objective-C by parasubvert on Friday June 13, 2014 @10:12AM (#7888910)
English book here (Canada link but should redirect you): https://itunes.apple.com/ca/book/using-swift-cocoa-objective...
Re:Using Swift with Cocoa and Objective-C by k-mcgrady on Friday June 13, 2014 @10:06AM (#7888882)
I'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.
Re:Using Swift with Cocoa and Objective-C by msie on Friday June 13, 2014 @01:07PM (#7889799)
I wish there was a properly formatted pdf version of the language guide. There is one floating around but the font size is too small.
Re:Using Swift with Cocoa and Objective-C by Nib on Friday June 13, 2014 @10:25AM (#7888982)
What language is this in?
Re:Using Swift with Cocoa and Objective-C by jvreeland on Friday June 13, 2014 @10:28AM (#7889002)
I 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.
Re:Using Swift with Cocoa and Objective-C by napolux on Friday June 13, 2014 @10:41AM (#7889059)
English
Re:Using Swift with Cocoa and Objective-C by thought_alarm on Friday June 13, 2014 @01:26PM (#7889898)
Swift.
Re:Using Swift with Cocoa and Objective-C by wil421 on Friday June 13, 2014 @10:06AM (#7888888)
This page is in Italian.
Re:Using Swift with Cocoa and Objective-C by jackjeff on Friday June 13, 2014 @10:12AM (#7888914)
It'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...

Re:Using Swift with Cocoa and Objective-C by wil421 on Friday June 13, 2014 @10:42AM (#7889064)
Thanks, pretty cool I just noticed if you change the /us/ part to any other country like cn, es, gb or it you will get the page in that language.
Re:Using Swift with Cocoa and Objective-C by k-mcgrady on Friday June 13, 2014 @10:55AM (#7889123)
If you just click on the big blue button it will open in the correct store for you location.
Re:Using Swift with Cocoa and Objective-C by wil421 on Friday June 13, 2014 @11:27AM (#7889294)
How would that help? The computer I am using doesnt have ibooks or itunes.
Re:Using Swift with Cocoa and Objective-C by k-mcgrady on Friday June 13, 2014 @11:52AM (#7889430)
It sounded like you...wanted to read the book which would require iBooks. I'm guessing you were just interested in an English translation of the summary?
Re:Using Swift with Cocoa and Objective-C by wil421 on Friday June 13, 2014 @12:45PM (#7889709)
Exactly.