Archive for June, 2015

Swift: Init a lovely day?

Swift 2.0 introduced .init, which offers functional access to initialization. You can construct an array of strings from integers by mapping String.init:

[1, 2, 3, 4, 5, 6].map(String.init)

Contrast with the old construction, which isn’t nearly as nice:

[1, 2, 3, 4, 5, 6].map{String($0)}

You can also populate enumerations with associated values. This example uses the Container enumeration I wrote about a few weeks ago.

Screen Shot 2015-06-25 at 10.42.46 AM

What you don’t want to do is use this as a long-hand for standard initialization. I think it’s a good rule to prefer String(5) over String.init(5).

Apple writes, “.init is still implicit when constructing using a static type, as in String(5). .init is required when using dynamic type objects, or when referring to the initializer as a function value.”

Update 1:

Trying to decide whether this is worth a radar:

Screen Shot 2015-06-25 at 10.58.30 AM

Update 2:

I oopsed on the initial post, using ()’s instead of {}’s for the 2nd map. But why does the first one work? (p.s. You want to debugPrint to see the quotation marks to confirm as String, not print)

Screen Shot 2015-06-25 at 12.58.48 PM

Animated out-of-office email

I suppose I knew that using animations in email was possible. I don’t think I’ve actually noticed an animation (outside of GIF attachments) until this week when I received an out-of-the-office response from an Apple evangelist.

Why don’t we see more visual accents like this? Maybe I just normally communicate with the wrong people.

#wrapper img#imageAccent.flyIn {
 -webkit-animation-name: 'flyIn'; 
 -webkit-animation-duration: 1.25s; 
 -webkit-animation-iteration-count: 1; 
 -webkit-animation-timing-function: ease-out; 
}

Fun with Apple Unicode: John Appleseed, author

Books

Who knew that John Appleseed was such a prolific author? Certainly not I! (And if you will forgive the pun, I wonder what the market price is for the one second from the right.)

Update: A thank you goes out to commenter Unicode.

📕
CLOSED BOOK
Unicode: U+1F4D5 (U+D83D U+DCD5), UTF-8: F0 9F 93 95

📗
GREEN BOOK
Unicode: U+1F4D7 (U+D83D U+DCD7), UTF-8: F0 9F 93 97

📘
BLUE BOOK
Unicode: U+1F4D8 (U+D83D U+DCD8), UTF-8: F0 9F 93 98

📙
ORANGE BOOK
Unicode: U+1F4D9 (U+D83D U+DCD9), UTF-8: F0 9F 93 99

 

Radars: I did not know that

I’m told by persons in the know that a radar’s title is editable by Apple staff and is commonly changed during handling. So summarizing in the title and then filling in all the other fields with “see title” is probably a bad strategy in general when filing bug reports.

Clarity also matters. Engineers really appreciate screenshots and movie clips that help demonstrate your problem as it’s sometimes hard to express what’s going on just in words.

System dumps can also be very handy. The sysdiagnose  command-line utility gathers system-wide diagnostic information, which you can attach to your radar.

Finally, don’t forget Open Radar. (FAQ)

Got some other radar tips? Let me know. I’ll update this post.

Playground Secrets and Power Tips 2nd edition

Screen Shot 2015-06-24 at 1.41.18 PM

 

Thank you to everyone who has bought the book and helped support this crazy project. The new edition will auto-deliver to everyone who  purchased the first edition in iTunes. I felt this was the best way to say thanks for buying the book and keeping faith with me.

Once the new edition arrives, I hope you will love it enough to tweet about it, write about it on Facebook, pass it to friends, and otherwise help it build an audience. I want to keep this project alive, updated, and growing. I can’t realistically do that without an expanding customer base. Every mention matters and is greatly appreciated.

I’m grateful to everyone buying my books and supporting my blog. It’s a crazy life and I hope you get as much out of this as I try to put into it.

Dear Erica: Extend Safe Index?

Dear Erica,

I’m trying to extend safe index to collection type instead of array. Can’t figure it out. Is this possible for collectionTypes in 2.0?

— Nathan Mann

Here’s my take. I’ve only given this the most minimal testing. Do please let me know how to improve on it!

// Thanks Jordan Rose for the IntegerType constraint
extension CollectionType where Index : IntegerType {
    subscript (safe index: UInt) ->
        Self.Generator.Element? {
        if Int(index) < numericCast(count) {
            return self[advance(startIndex,
                numericCast(index))]
        }
        return nil
    }
}
// Thanks Brent Royal-Gordon 
extension CollectionType where Index: Comparable {
    subscript (safe index: Index) -> Generator.Element? {
        guard startIndex <= index && index < endIndex else {
            return nil
        }
        return self[index]
    }
}