- Don't add code cruft. Avoid parentheses around conditions in if-statements or with the
returnkeyword. Don't add semicolons except where syntactically demanded in statements or to separate statements on the same line. - Don't use ALL_CAPS; use camelCase
- Don't fight type inference. Use enumeration prefixes, self-references, and class names (with constructors) only when necessary or to clarify coding intent.
- Don't use
varwhenletis appropriate, especially for properties. The compiler better optimizesletstatements for items whose values will not change during their lifetime. For example, Apple writes, "It is good practice to create immutable collections in all cases where the collection does not need to change. Doing so enables the Swift compiler to optimize the performance of the collections you create." - Don't use classes when structs will do. Use class
"JUST BECAUSE YOU'RE USING SWIFT DOESN'T MEAN YOUR QUESTION IS ABOUT SWIFT"
Ask your question and then be patient. Tell us what you want to happen, what is actually happening, and include any error messages you find:
- Provide a scenario. "I am trying to do X, I do so by calling APIs W and Y, but Z happens instead. I see the following error messages:..."
- Focus on the goal. Ask about what you're trying to achieve (the big story) rather than the specific technical detail that you believe is holding you back.
- Don't solicit help. Don't say, "Does anyone here know about (for example) Protocol Extensions". Just ask your question.
- Do your homework. Search the web before asking in-channel.
- Be courteous Don't just paste Stack Overflow questions in-channel.
- Remember the topic Refer questions about third party libraries to their developers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// Observes a run loop to detect any stalling or blocking that occurs. | |
| /// | |
| /// This class is thread-safe. | |
| @interface GHRunLoopWatchdog : NSObject | |
| /// Initializes the receiver to watch the specified run loop, using a default | |
| /// stalling threshold. | |
| - (id)initWithRunLoop:(CFRunLoopRef)runLoop; | |
| /// Initializes the receiver to detect when the specified run loop blocks for |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // MultiDirectionAdjudicatingScrollView.swift | |
| // Khan Academy | |
| // | |
| // Created by Andy Matuschak on 12/16/14. | |
| // Copyright (c) 2014 Khan Academy. All rights reserved. | |
| // | |
| import UIKit | |
| import UIKit.UIGestureRecognizerSubclass |
See this SO discussion.
- https://github.com/simonwhitaker/swmath — define new functions which utilize clang function overloading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Playground - noun: a place where people can play | |
| import UIKit | |
| var str = "Hello, playground" | |
| enum TimeIntervalUnit { | |
| case Seconds, Minutes, Hours, Days, Months, Years | |
| func dateComponents(interval: Int) -> NSDateComponents { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #define COMPARE(A, B) ({ \ | |
| __typeof__(A) __a = (A); \ | |
| __typeof__(B) __b = (B); \ | |
| __a < __b ? NSOrderedAscending : __a == __b ? NSOrderedSame : NSOrderedDescending; \ | |
| }) |
Generate the list yourself:
$ cd /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS*.sdk/System/Library/Frameworks/UIKit.framework/Headers
$ grep UI_APPEARANCE_SELECTOR ./* | \
sed 's/NS_AVAILABLE_IOS(.*)//g' | \
sed 's/NS_DEPRECATED_IOS(.*)//g' | \
sed 's/API_AVAILABLE(.*)//g' | \
sed 's/API_UNAVAILABLE(.*)//g' | \
sed 's/UI_APPEARANCE_SELECTOR//g' | \
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Several macros simplifying use of weak references to self inside blocks | |
| * which goal is to reduce risk of retain cycles. | |
| * | |
| * Example: | |
| * @code | |
| @interface Example : NSObject{ | |
| int _i; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var Dictionary; | |
| if (!Dictionary) { | |
| Dictionary = {}; | |
| } | |
| (function () { | |
| 'use strict'; | |
| function f(n) { | |
| // Format integers to have at least two digits. |