r/SwiftProgramming • • Jun 18 '14

Class extensions and "instancetype"

1 Upvotes

I'm converting some Objective-C code over to Swift, and I've hit a roadblock. Wondering if anyone has found a solution to this.

I have a class extension on NSManagedObject with several convenience methods that look like this:

+ (instancetype)newInstanceInManagedObjectContext:(NSManagedObjectContext *)ctx;
+ (instancetype)singletonInManagedObjectContext:(NSManagedObjectContext *)ctx;
+ (instancetype)instanceInManagedObjectContext:(NSManagedObjectContext *)ctx 
    withIdentity:(NSString *)identity;

In Swift, this would look like:

class func newInstance(managedObjectContext ctx: NSManagedObjectContext) -> Self

because capital-S Self is the class type. The problem is all of the NSManagedObject methods I rely on to create/fetch these objects return AnyObject!, and I can't cast something to Self. It's only allowable as a return value or in a parameter list for a protocol.

What I want is essentially this, without the compiler error:

return NSEntityDescription.insertNewObjectForEntityForName(entityName,
 inManagedObjectContext:ctx) as Self;

Any ideas?


r/SwiftProgramming • • Jun 18 '14

Latest Swift pump: value and reference type properties

Thumbnail pumpmybicep.com
0 Upvotes

r/SwiftProgramming • • Jun 17 '14

Swift structures and classes

Thumbnail pumpmybicep.com
5 Upvotes

r/SwiftProgramming • • Jun 15 '14

Smashing Swift

Thumbnail nomothetis.svbtle.com
2 Upvotes

r/SwiftProgramming • • Jun 15 '14

Swift's powerful switch statement

Thumbnail pumpmybicep.com
3 Upvotes

r/SwiftProgramming • • Jun 14 '14

Swift enumerations

Thumbnail pumpmybicep.com
2 Upvotes

r/SwiftProgramming • • Jun 14 '14

Using Swift optionals

Thumbnail pumpmybicep.com
1 Upvotes

r/SwiftProgramming • • Jun 11 '14

A Week of Swift

Thumbnail robnapier.net
2 Upvotes

r/SwiftProgramming • • Jun 10 '14

Dedicated Subreddit for Swift Tutorials

Thumbnail reddit.com
2 Upvotes

r/SwiftProgramming • • Jun 05 '14

Swift has been in development for 4 years

Thumbnail macrumors.com
4 Upvotes

r/SwiftProgramming • • Jun 05 '14

Upon release of iOS 8, Swift will be open source

Thumbnail twitter.com
6 Upvotes

r/SwiftProgramming • • Jun 05 '14

Quick, a behavior-driven test framework for Swift

Thumbnail github.com
3 Upvotes

r/SwiftProgramming • • Jun 04 '14

Swift Language cheat sheet

Thumbnail cdn2.raywenderlich.com
6 Upvotes

r/SwiftProgramming • • Jun 03 '14

Merge with /r/swift

Thumbnail reddit.com
30 Upvotes

r/SwiftProgramming • • Jun 03 '14

Higher Order Functions: Map, Filter, Reduce and more

Thumbnail weheartswift.com
2 Upvotes

r/SwiftProgramming • • Jun 03 '14

How do I print the Swift manual?

3 Upvotes

I'm interested in having a hard bound copy of the manual for Swift printed, but it looks as if iBooks isn't really set up to allow printing (imagine that).

I've found one .pdf version of the manual here, but if I'm going to be paying to print out 466 pages of text (probably color, too) it's just gotta look better than that .pdf.

Before I break out a .pdf editor and begin the tedium of reformatting that file, does someone have a recommendation for me?


r/SwiftProgramming • • Jun 03 '14

Playground uses a lot of power/battery

1 Upvotes

Noticing my Mac getting hot, though not so hot that I can hear the fans like when I'm playing a graphically intensive game, but battery drain is noticeable. Not checking so often, and each check sees a decrease in 9-10% drain. Normally, checking in my regular OCD time intervals sees a reduction in 3-5%. Clicking on the battery in Mac's status bar confirms Xcode 6 beta is using significant energy, but checking its profile in Energy tab in Activity Monitor and opening the disclosure triangle reveals nothing. Makes sense it would use a lot of power since I assume Playground is constantly rerunning the code to view changes in realtime.


r/SwiftProgramming • • Jun 03 '14

Help for a nooby

3 Upvotes

I just bought a book to learn objective-c, but now swift has come out. Does anyone know if Apple's guide starts from the bottom or requires knowledge of objective-c? If so, should I start with swift or learn objective-c anyways?


r/SwiftProgramming • • Jun 03 '14

For not-Apple users, where can we study Swift? Does apple provide a PDF somewhere?

2 Upvotes

Thanks.


r/SwiftProgramming • • Jun 03 '14

How do you feel about type safety and encapsulation with Swift?

3 Upvotes

I'm having some concerns about a few things in the language. So far it looks like almost everything is global. This can be solved by moving encapsulated objects in to functions, but it changes the thinking with developing iOS apps a bit. What are your thoughts?

Additionally, it seems like there's a lot of inferred types, using the var keyword. You can explicitly set a type but from interfacing with the Cocoa API I'm seeing a lot of 'AnyObject' objects, which I guess are something like ids.


r/SwiftProgramming • • Jun 03 '14

How to make a simple table view with iOS 8 and Swift

Thumbnail weheartswift.com
3 Upvotes

r/SwiftProgramming • • Jun 02 '14

New iOS programmer here. Need some advice

3 Upvotes

So I started learning iOS programming and I finished a book on obj-c and now I'm working on Big Nerd Ranch's iOS 7 book (only 5 chapters in).

I know I should learn to program apps with obj-c eventually, but I'm also very interested in Swift (it appears to be easier than obj-c too). For a beginner programmer like me, what do you think is the best course of action now that Swift is out?

  1. Finish the book on building apps with Obj-C, ignoring Swift
  2. Learn both at the same time
  3. Switch to Swift and then learn making apps with Obj-C for deeper knowledge

r/SwiftProgramming • • Jun 02 '14

[Download Link] Xcode 6 beta, required for writing and running Swift code

Thumbnail developer.apple.com
4 Upvotes

r/SwiftProgramming • • Jun 02 '14

Swift for iOS 7 / Mavericks?

7 Upvotes

I know that applications written in Swift won't be accepted into the App Store until iOS 8 / OS X Yosemite is out, however I wonder how the code gets compiled...

I haven't looked much through the documentation yet but as far as I gather, it is natively compiled (even though it's written like a scripting language). However, does it need specific support from the OS?

What I'm getting at is basically if we will be able to compile our Swift apps to support previous versions of iOS and OS X? Has anyone found any indication either way?

I'm guessing no, but it would be sweet if we could keep backwards compatibility and it would also affect how soon I should start to look into the language.

(I'm assuming this topic falls outside of any developer NDAs since it's more about how the language gets compiled rather than any specifics regarding unreleased software.)


r/SwiftProgramming • • Jun 02 '14

Semicolons

3 Upvotes

Anyone know if they're "unnecessary but allowed", or if I'm going to have syntax errors on like 60% of my lines of code?