Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
lord funk
Feb 16, 2004

Huge congrats, rjmccall! Really excited to dig into Swift.

Adbot
ADBOT LOVES YOU

lord funk
Feb 16, 2004

You use true and false instead of YES and NO. I liked YES and NO. :(

lord funk
Feb 16, 2004

rjmccall posted:

(Advanced Swift, Thursday at 11:30)
:dance:

Stupid question for anyone: how exactly do I open the Swift Programming Language iBook in Playground?

The Swift Programming Language posted:

NOTE

For the best experience, open this chapter as a playground in Xcode. Playgrounds allow you to edit the code listings and see the result immediately.

lord funk
Feb 16, 2004

Plorkyeran posted:

At the moment trying to learn Swift without knowing Objective-C is probably not going to end well, but long term it seems perfectly reasonable to learn it without knowing C or Objective-C first, although you'll probably end up needing to know all three to write anything substantial.

I disagree. I think if your goal is to learn Swift you aren't going to get much benefit from trudging through ObjC. Plus the pedagogical advantage of learning in the Playground environment is fantastic.

lord funk
Feb 16, 2004

Plorkyeran posted:

If your goal is to just learn Swift then sure, you won't need to ever learn objective-c once the docs are fully in place. If your goal is to actually write nontrivial apps in Swift, you'll need to know Objective-C at least well enough to debug issues when using Objective-C libraries.

I look at it from the same perspective as what EL BROMANCE said about the books that insisted that you needed a solid C foundation. I skipped that when learning ObjC, and I work with old C frameworks like CoreAudio all the time. You just kind of look up what you need when you get there. :shrug:

I'm really excited for the talk about integrating Swift with ObjC.

Question for rjmccall: will there be a Swift version of sets?

lord funk
Feb 16, 2004

Doctor w-rw-rw- posted:

Swift code:
// Playground - noun: a place where people can play

import Cocoa


func foo(inout x:Array<String>) -> Array<String> {
    x[0] = "qwer"
    return x
}

foo(["asdf", "1234"])
This crashes my Xcode. Known problem? Anyone else able to reproduce?
Yeah me too.

lord funk
Feb 16, 2004

If Swift is a recent name, what did you guys call the language internally while you were developing it?

lord funk
Feb 16, 2004

code:
occupations["Jayne"] = "Public Relations"
Best part of The Swift Programming Language.

lord funk
Feb 16, 2004

Advanced Swift talk was great, rjmccall. It sounded like you guys could have used another session or two to get to everything you wanted to talk about.

lord funk
Feb 16, 2004

May be a dumb question, but is there a possibility of an Xcode Edit >> Refactor >> Convert to Swift... in the future?

lord funk
Feb 16, 2004

I got excited after seeing all the cool projection of Swift -> ObjC / ObjC -> Swift stuff. I figure if that much work is already done, it might not be too far of a leap to just translate an implementation completely.

lord funk
Feb 16, 2004

Are property observers meant to substitute for key value observation in ObjC? It seems to me that they're very local, like having a class observe itself, but not able to be used for MVC coordination the way I've been using KVO.

lord funk
Feb 16, 2004

The required type casting is feeling brutal. For a function that takes a CGFloat:
code:
CGFloat(fmaxf(CFloat(CGFloat(offset) / view.bounds.width), 0.0))
Old style
code:
fmaxf(offset / view.bounds.width, 0.0)
Any technique to mitigate this that I'm missing? Or am I just whining. :v:

lord funk
Feb 16, 2004

That worked, thanks.

Gul Banana posted:

not tested, i'm at a PC. i'm also not sure if there's a way to limit the scope of extensions.. ideally this would be something you import for a specific source file or module.

Can you explain why it would be bad to have this project wide? If it's a valid conversion, what's the harm?

lord funk
Feb 16, 2004

dizzywhip posted:

I'm super happy about the private(set) access modifier, I was really hoping for something like that.

Can you quick explain this? I'm falling behind in my Swift blog reading / version updates.

Speaking of, which blogs are you all reading?

lord funk
Feb 16, 2004

Regarding lazy loading:

In some cases I've found it's nice to allow for recalculation of a property by nilling out its backing store. So in Obj-C:

Objective-C code:
- (NSArray *)someArray
{
    if (!_someArray) {
        //calculate the goods
    }
    return _someArray;
}
That array would be used until some later point where the data might change. You could then nil out _someArray, and redo the lazy loading later. My questions are:

1. Since lazy properties are only calculated once, how could this be done in Swift?
2. Or is this a bad habit of mine and there's a better way?

lord funk
Feb 16, 2004

How do you write out Swift methods? For example, if I say viewDidLoad() that makes sense. Same with UIImage(named:). But what about UIButton's set image for control state? Is it:

setImage(forState:)
or
setImage(image:forState:)

The documentation uses the Obj-C way of -setImage:forState: which doesn't seem right.

lord funk
Feb 16, 2004

Is there a particular reason why arrays don't have a removeObject: method like NSArray? Is it a safety thing?

lord funk
Feb 16, 2004

Ran into some bad code from a student of mine, but I was wondering why it doesn't throw an exception about the named variable 'index' being used twice:

code:
if let index = find(self.someArray, someObject) {
    for index in self.someArray {
        
    }
}
Tried another scenario that I thought would throw an exception but doesn't:
code:
for object in self.someArray {
    for object in self.otherArray {
        
    }
}

lord funk
Feb 16, 2004

Wow never noticed that before in any other languages. Thanks for the example, that one makes a lot more sense.

lord funk
Feb 16, 2004

Not sure if this is a style question or not. Is there any functional difference between referring to properties with self.property vs. just property? I use the former because I think it clarifies that it's a class property and not just a local variable, but I'm wondering if others are doing the same.

lord funk
Feb 16, 2004

How would you filter a String to only have upper and lowercase letters? I'm trying to iterate through an NSCharacterSet and use stringByReplacingOccurrencesOfString:withString: to do this, but my brain is failing me.

lord funk
Feb 16, 2004

pokeyman posted:

If you like NSScanner, you could do something like

Thanks, that's great. It's just to clean up a textfield input, but like everything Swift I feel like I'm missing something when I rely on NSString methods / scanners / character sets, etc.

lord funk
Feb 16, 2004

I gave up trying to use lldb to drill down at all in Swift. I would show students breakpoints and 'po' but it would just hang for 10 seconds and then spit out gibberish, every time.

lord funk
Feb 16, 2004

Swift Blog posted:

let constants are now more powerful and consistent — The new rule is that a let constant must be initialized before use (like a var), and that it may only be initialized, not reassigned or mutated after initialization.

:dance:

Actually, :dance: for everything in the post.

lord funk
Feb 16, 2004

me: UIColor *color = [uic
Xcode: I know! UICollectionElementKindSectionFooter!
me: :stare:

lord funk
Feb 16, 2004

Hey congrats on being the 'most loved' language on the Stack Overflow survey.

lord funk
Feb 16, 2004

Biggest applause break 2 years running :toot:

lord funk
Feb 16, 2004

KidDynamite posted:

Is that rjmcall with the teapot shirt?

Yeah and that shirt is great. Nice presentation!

lord funk
Feb 16, 2004

I've also been playing with protocol extensions since the WWDC talk. I can get a heterogeneous array of protocol adhering objects:

code:
var array: [MyProtocol] = [ ... ]
But I can't get a set to work:

code:
var set: Set<MyProtocol> = [ ... ]

error: Type 'MyProtocol' does not conform to protocol 'Hashable'
Is this solvable with a protocol extension?

lord funk
Feb 16, 2004

But wasn't that what Protocol-Oriented Programming in Swift was all about? Solving the Equatable issue by using protocol extensions so we can move forward with this kind of use?

lord funk
Feb 16, 2004

Is it possible to store an array of class types? I have a group of classes that conform to a protocol which declares a static variable.

code:
protocol ThingConforming {
    static var aThing: String { get }
}

class Foo: ThingConforming {
    static var aThing: String = "FooThing"
}

class Bar: ThingConforming {
    static var aThing: String = "BarThing"
}
...
What I would like to have is an array that holds the class types themselves, so I can iterate over all the classes and check their static variable:

code:
let classes = [Foo, Bar]
for class in classes {
    //check aThing
}

lord funk
Feb 16, 2004

pokeyman posted:

Does let classes = [Foo.self, Bar.self] work?

Yes! Requires Swift 2 to access the properties, but it works:

code:
let classes: [ThingConforming.Type] = [Foo.self, Bar.self]

lord funk
Feb 16, 2004

Can you access a protocol's static variable in a protocol extension method?

code:
protocol SomeProtocol {
    static var typeVar: String { get }
}

extension SomeProtocol {
    func method() {
        //how to get to type's typeVar?
    }
}

lord funk
Feb 16, 2004

rjmccall posted:

Swift code:
let string = Self.typeVar

I swear I tried this. =/ Maybe I just let it autocomplete to self. Ahh.

lord funk
Feb 16, 2004

Will enum values ever autocomplete in the shorthand form? I've been hoping for this since the syntax was first shown.

I want

code:
let button = UIButton(type: .cu
to autocomplete to

code:
let button = UIButton(type: .Custom

lord funk
Feb 16, 2004

Is there something other than using po in the debugger that I should be aware of? po is constantly finding new ways to not show me anything at all, even as the local variable list shows me the objects I want to inspect.

lord funk
Feb 16, 2004

Syntax head scratcher:

In Darwin >> C >> Math:

code:
public func __sincospif(__x: Float, _ __sinp: UnsafeMutablePointer<Float>, _ __cosp: UnsafeMutablePointer<Float>)
My call:

code:
    var a = angle
    var c: Float = 0.0
    var s: Float = 0.0
    
    __sincospif(a, UnsafeMutablePointer<Float>(s), UnsafeMutablePointer<Float>(c))
Error:
code:
Cannot invoke '__sincospif' with an argument list of type '(Float, UnsafeMutablePointer<Float>, UnsafeMutablePointer<Float>)'

lord funk
Feb 16, 2004

Is it possible to share a struct type between a Metal shader file (C++) and the rest of my Swift code that uses SIMD vector types? Swift 2 supports SIMD, and you can import it into the shader file, but I can't seem to bridge the two so I can just create one struct that they both see.

Adbot
ADBOT LOVES YOU

lord funk
Feb 16, 2004

Please school me on retain / release in array iterations.

Here is a test app with a big array that I iterate through each frame:

code:
class TestClass {
    var value: Double = 0.0
    func calculate() {
        value = Double(random())
    }
}

class ViewController: UIViewController {
    
    let testArray: [TestClass] = {
        var build: [TestClass] = []
        for _ in 0..<100000 {
            let t = TestClass()
            build.append(t)
        }
        return build
    }()

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        let displayLink = CADisplayLink(target: self, selector: "displayLinkTicked")
        displayLink.addToRunLoop(NSRunLoop.mainRunLoop(), forMode: NSRunLoopCommonModes)
    }

    func displayLinkTicked() {
        for testClass in testArray {
            testClass.calculate()
        }
    }
}
I was surprised to see that the majority of the work done in the loop is retain / release calls (93%):



My assumption is that this is ARC doing its thing. If this were Objective-C, I'd disable ARC for the file to avoid this. What are my options in Swift? It seems really wasteful to retain / release each object when I know they aren't going anywhere.

  • Locked thread