Share
Embed
UIKonf 2015: iOS API Design
A talk really isn't a talk without presenter notes and animations! For those, check out the Keynote file here: https://github.com/modocache/talks/blob/master/20150519-uikonf/ios-api-design.key
Brian Gesiak
May 19, 2015
Transcript
141 slides
-
@interface BananaAlertView : UIAlertView + (void)showWithTitleText:(NSString *)titleText messageText:(NSString *)messageText dismissButtonText:(NSString
*)dismissButtonText; @end tintColor:(UIColor *)tintColor; -
[BananaAlertView showWithTitleText:@"403 Error" messageText:@"Forbidden banana." dismissButtonText:@"Dang."]; No known class method
for selector 'showWithTitleText:messageText:…' -
[BananaAlertView showWithTitleText:@"403 Error" messageText:@"Forbidden banana." dismissButtonText:@"Dang."]; No known class method
for selector 'showWithTitleText:messageText:…' -
@interface BananaAlertView : UIAlertView + (void)showWithTitleText:(NSString *)titleText messageText:(NSString *)messageText dismissButtonText:(NSString
*)dismissButtonText; + (void)showWithTitleText:(NSString *)titleText messageText:(NSString *)messageText dismissButtonText:(NSString *)dismissButtonText tintColor:(UIColor *)tintColor; @end -
@interface BananaAlertView : UIAlertView + (void)showWithTitleText:(NSString *)titleText messageText:(NSString *)messageText dismissButtonText:(NSString
*)dismissButtonText; + (void)showWithTitleText:(NSString *)titleText messageText:(NSString *)messageText dismissButtonText:(NSString *)dismissButtonText tintColor:(UIColor *)tintColor; @end -
@implementation BananaAlertView + (void)showWithTitleText:(NSString *)titleText messageText:(NSString *)messageText dismissButtonText:(NSString *)dismissButtonText {
[self showWithTitleText:titleText messageText:messageText dismissButtonText:dismissButtonText tintColor:[UIColor yellowColor]]; } + (void)showWithTitleText:(NSString *)titleText messageText:(NSString *)messageText dismissButtonText:(NSString *)dismissButtonText tintColor:(UIColor *)tintColor { // ... } -
@implementation BananaAlertView + (void)showWithTitleText:(NSString *)titleText messageText:(NSString *)messageText dismissButtonText:(NSString *)dismissButtonText {
[self showWithTitleText:titleText messageText:messageText dismissButtonText:dismissButtonText tintColor:[UIColor yellowColor]]; } + (void)showWithTitleText:(NSString *)titleText messageText:(NSString *)messageText dismissButtonText:(NSString *)dismissButtonText tintColor:(UIColor *)tintColor { // ... } -
@implementation BananaAlertView + (void)showWithTitleText:(NSString *)titleText messageText:(NSString *)messageText dismissButtonText:(NSString *)dismissButtonText {
[self showWithTitleText:titleText messageText:messageText dismissButtonText:dismissButtonText tintColor:[UIColor yellowColor]]; } + (void)showWithTitleText:(NSString *)titleText messageText:(NSString *)messageText dismissButtonText:(NSString *)dismissButtonText tintColor:(UIColor *)tintColor { // ... } -
[BananaAlertView showWithTitleText:@"403 Error" messageText:@"Forbidden banana." dismissButtonText:@"Dang."]; [BananaAlertView showWithTitleText:@"500 Error" messageText:@"Internal
banana." dismissButtonText:@"Rats." tintColor:[UIColor redColor]]; -
public class BananaAlertView { public static func show( titleText: String,
messageText: String, dismissButtonText: String) { // ... } } -
public class BananaAlertView { public static func show( titleText: String,
messageText: String, dismissButtonText: String) { // ... } } dismissButtonText: String, tintColor: UIColor = UIColor.yellowColor()) { // ... } -
public typealias ButtonCallback = (buttonIndex: Int) -> () public class
BananaAlertView { public static func show( titleText: String, messageText: String, dismissButtonText: String, dismissButtonCallback: ButtonCallback, tintColor: UIColor = UIColor.yellowColor()) { // ... } } -
public typealias ButtonCallback = (buttonIndex: Int) -> () public class
BananaAlertView { public static func show( titleText: String, messageText: String, dismissButtonText: String, dismissButtonCallback: ButtonCallback, tintColor: UIColor = UIColor.yellowColor()) { // ... } } -
public typealias ButtonCallback = (buttonIndex: Int) -> () public class
BananaAlertView { public static func show( titleText: String, messageText: String, dismissButtonText: String, dismissButtonCallback: ButtonCallback, tintColor: UIColor = UIColor.yellowColor()) { // ... } } -
BananaAlertView.show( "500 Error", messageText: "Internal banana error.", dismissButtonText: "Rats.", dismissButtonCallback:
{ (buttonIndex: Int) in // ... }, tintColor: UIColor.redColor() ) -
BananaAlertView.show( "500 Error", messageText: "Internal banana error.", dismissButtonText: "Rats.", dismissButtonCallback:
{ (buttonIndex: Int) in // ... }, tintColor: UIColor.redColor() ) -
public typealias ButtonCallback = (buttonIndex: Int) -> () public class
BananaAlertView { public static func show( titleText: String, messageText: String, dismissButtonText: String, dismissButtonCallback: ButtonCallback, tintColor: UIColor = UIColor.yellowColor()) { // ... } } -
public typealias ButtonCallback = (buttonIndex: Int) -> () public class
BananaAlertView { public static func show( titleText: String, messageText: String, dismissButtonText: String, dismissButtonCallback: ButtonCallback, tintColor: UIColor = UIColor.yellowColor()) { // ... } } (buttonIndex: Int, buttonTitle: String) -> () -
BananaAlertView.show( "500 Error", messageText: "Internal banana error.", dismissButtonText: "Rats.", dismissButtonCallback:
{ (buttonIndex: Int) in // ... }, tintColor: UIColor.redColor() ) -
BananaAlertView.show( "500 Error", messageText: "Internal banana error.", dismissButtonText: "Rats.", dismissButtonCallback:
{ (buttonIndex: Int) in // ... }, tintColor: UIColor.redColor() ) Cannot invoke 'show' with an argument list of type '(String, message… -
BananaAlertView.show( "500 Error", messageText: "Internal banana error.", dismissButtonText: "Rats.", dismissButtonCallback:
{ (buttonIndex: Int) in // ... }, tintColor: UIColor.redColor() ) Cannot invoke 'show' with an argument list of type '(String, message… -
public struct ButtonCallbackParameters { let buttonIndex: Int let buttonText: String
} public typealias ButtonCallback = (buttonIndex: Int) -> () -
public struct ButtonCallbackParameters { let buttonIndex: Int let buttonText: String
} public typealias ButtonCallback = (buttonIndex: Int) -> () -
public struct ButtonCallbackParameters { let buttonIndex: Int let buttonText: String
} public typealias ButtonCallback = (buttonIndex: Int) -> () public typealias ButtonCallback = (parameters: ButtonCallbackParameters) -> () -
BananaAlertView.show( "500 Error", messageText: "Internal banana error.", dismissButtonText: "Rats.", dismissButtonCallback:
{ (buttonIndex: Int) in // ... }, tintColor: UIColor.redColor() ) -
BananaAlertView.show( "500 Error", messageText: "Internal banana error.", dismissButtonText: "Rats.", dismissButtonCallback:
{ (buttonIndex: Int) in // ... }, tintColor: UIColor.redColor() ) dismissButtonCallback: { parameters in if parameters.buttonText == "Rats." { // ... } }, -
BananaAlertView.show( "500 Error", messageText: "Internal banana error.", dismissButtonText: "Rats.", dismissButtonCallback:
{ (buttonIndex: Int) in // ... }, tintColor: UIColor.redColor() ) dismissButtonCallback: { parameters in if parameters.buttonText == "Rats." { // ... } }, -
public struct ButtonCallbackParameters { let buttonIndex: Int let buttonTitle: String
} public typealias ButtonCallback = (parameters: ButtonCallbackParameters) -> () -
public struct ButtonCallbackParameters { let buttonIndex: Int let buttonTitle: String
} public typealias ButtonCallback = (parameters: ButtonCallbackParameters) -> () let totalButtonCount: Int -
public struct ButtonCallbackParameters { let buttonIndex: Int let buttonTitle: String
} public typealias ButtonCallback = (parameters: ButtonCallbackParameters) -> () let totalButtonCount: Int -
BananaAlertView.show( "500 Error", messageText: "Internal banana error.", dismissButtonText: "Rats.", dismissButtonCallback:
{ parameters in if parameters.buttonText == "Rats." { // ... } }, tintColor: UIColor.redColor() ) -
BananaAlertView.show( "500 Error", messageText: "Internal banana error.", dismissButtonText: "Rats.", dismissButtonCallback:
{ parameters in if parameters.buttonText == "Rats." { // ... } }, tintColor: UIColor.redColor() ) -
public struct ButtonCallbackParameters { let buttonIndex: Int let buttonTitle: String
let totalButtonCount: Int } public typealias ButtonCallback = (parameters: ButtonCallbackParameters) -> () -
public struct ButtonCallbackParameters { let buttonIndex: Int let buttonTitle: String
let totalButtonCount: Int } public typealias ButtonCallback = (parameters: ButtonCallbackParameters) -> () @availability(*, deprecated=2.0) -
BananaAlertView.show( "500 Error", messageText: "Internal banana error.", dismissButtonText: "Rats.", dismissButtonCallback:
{ parameters in if parameters.buttonText == "Rats." { // ... } }, tintColor: UIColor.redColor() ) -
BananaAlertView.show( "500 Error", messageText: "Internal banana error.", dismissButtonText: "Rats.", dismissButtonCallback:
{ parameters in if parameters.buttonText == "Rats." { // ... } }, tintColor: UIColor.redColor() ) 'buttonText' was deprecated in * version 2.0 -
public struct AlertViewOptions { public let dismissButtonText: String public let
tintColor: UIColor public let animationDuration: NSTimeInterval } public class BananaAlertView { public static func show( titleText: String, messageText: String, options: AlertViewOptions) { // ... } } -
public struct AlertViewOptions { public let dismissButtonText: String public let
tintColor: UIColor public let animationDuration: NSTimeInterval } public class BananaAlertView { public static func show( titleText: String, messageText: String, options: AlertViewOptions) { // ... } } = AlertViewOptions()) { = "OK" = UIColor.yellowColor() = 1 -
@interface JVFloatLabeledTextField : UITextField @property (nonatomic, assign) CGFloat floatingLabelYPadding UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIFont *floatingLabelFont UI_APPEARANCE_SELECTOR; @property (nonatomic, strong) UIColor *floatingLabelTextColor UI_APPEARANCE_SELECTOR; @end -
@interface JVFloatLabeledTextField : UITextField @property (nonatomic, assign) CGFloat floatingLabelYPadding UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIFont *floatingLabelFont UI_APPEARANCE_SELECTOR; @property (nonatomic, strong) UIColor *floatingLabelTextColor UI_APPEARANCE_SELECTOR; @end -
CGRect frame = CGRectMake(0.f, 0.f, 100.f, 44.f); JVFloatLabeledTextField *textField =
[[JVFloatLabeledTextField alloc] initWithFrame:frame]; -
CGRect frame = CGRectMake(0.f, 0.f, 100.f, 44.f); JVFloatLabeledTextField *textField =
[[JVFloatLabeledTextField alloc] initWithFrame:frame]; textField.placeholder = @"Price"; textField.floatingLabelYPadding = 10.f; textField.floatingLabelTextColor = [UIColor blueColor]; -
CGRect frame = CGRectMake(0.f, 0.f, 100.f, 44.f); JVFloatLabeledTextField *textField =
[[JVFloatLabeledTextField alloc] initWithFrame:frame]; textField.placeholder = @"Price"; textField.floatingLabelYPadding = 10.f; textField.floatingLabelTextColor = [UIColor blueColor]; -
@interface UITextField (JVFloatLabeledTextField) @property (nonatomic, assign) CGFloat floatingLabelYPadding UI_APPEARANCE_SELECTOR; @property
(nonatomic, strong) UIFont *floatingLabelFont UI_APPEARANCE_SELECTOR; @property (nonatomic, strong) UIColor *floatingLabelTextColor UI_APPEARANCE_SELECTOR; @end @interface JVFloatLabeledTextField : UITextField -
@interface UITextField (JVFloatLabeledTextField) @property (nonatomic, assign) CGFloat floatingLabelYPadding UI_APPEARANCE_SELECTOR; @property
(nonatomic, strong) UIFont *floatingLabelFont UI_APPEARANCE_SELECTOR; @property (nonatomic, strong) UIColor *floatingLabelTextColor UI_APPEARANCE_SELECTOR; @end -
@interface UITextField (JVFloatLabeledTextField) @property (nonatomic, assign) CGFloat floatingLabelYPadding UI_APPEARANCE_SELECTOR; @property
(nonatomic, strong) UIFont *floatingLabelFont UI_APPEARANCE_SELECTOR; @property (nonatomic, strong) UIColor *floatingLabelTextColor UI_APPEARANCE_SELECTOR; @end -
@interface UITextField (JVFloatLabeledTextField) { CGFloat _floatingLabelYPadding; } @property (nonatomic, assign)
CGFloat floatingLabelYPadding UI_APPEARANCE_SELECTOR; @end -
@interface UITextField (JVFloatLabeledTextField) { CGFloat _floatingLabelYPadding; } @property (nonatomic, assign)
CGFloat floatingLabelYPadding UI_APPEARANCE_SELECTOR; @end -
const void * const YPaddingKey = &YPaddingKey; - (void)setFloatingLabelYPadding:(CGFloat)yPadding {
objc_setAssociatedObject( self, YPaddingKey, @(yPadding), OBJC_ASSOCIATION_RETAIN_NONATOMIC ); } - (CGFloat)floatingLabelYPadding { return [objc_getAssociatedObject(self, YPaddingKey) floatValue]; } -
const void * const YPaddingKey = &YPaddingKey; - (void)setFloatingLabelYPadding:(CGFloat)yPadding {
objc_setAssociatedObject( self, YPaddingKey, @(yPadding), OBJC_ASSOCIATION_RETAIN_NONATOMIC ); } - (CGFloat)floatingLabelYPadding { return [objc_getAssociatedObject(self, YPaddingKey) floatValue]; } -
const void * const YPaddingKey = &YPaddingKey; - (void)setFloatingLabelYPadding:(CGFloat)yPadding {
objc_setAssociatedObject( self, YPaddingKey, @(yPadding), OBJC_ASSOCIATION_RETAIN_NONATOMIC ); } - (CGFloat)floatingLabelYPadding { return [objc_getAssociatedObject(self, YPaddingKey) floatValue]; } -
@interface UITextField (JVFloatLabeledTextField) @property (nonatomic, assign) CGFloat floatingLabelYPadding UI_APPEARANCE_SELECTOR; @property
(nonatomic, strong) UIFont *floatingLabelFont UI_APPEARANCE_SELECTOR; @property (nonatomic, strong) UIColor *floatingLabelTextColor UI_APPEARANCE_SELECTOR; @end -
@interface UITextField (JVFloatLabeledTextField) @property (nonatomic, assign) CGFloat floatingLabelYPadding UI_APPEARANCE_SELECTOR; @property
(nonatomic, strong) UIFont *floatingLabelFont UI_APPEARANCE_SELECTOR; @property (nonatomic, strong) UIColor *floatingLabelTextColor UI_APPEARANCE_SELECTOR; @end -
const void * const YPaddingKey = &YPaddingKey; const void *
const FontKey = &FontKey; const void * const TextColorKey = &TextColorKey; - (void)setFloatingLabelYPadding:(CGFloat)yPadding { objc_setAssociatedObject( self, YPaddingKey, @(yPadding), OBJC_ASSOCIATION_RETAIN_NONATOMIC ); } - (CGFloat)floatingLabelYPadding { return [objc_getAssociatedObject(self, YPaddingKey) floatValue]; -
} - (UIFont *)floatingLabelFont { return objc_getAssociatedObject(self, FontKey); } -
(void)setFloatingLabelTextColor:(UIColor *)color { objc_setAssociatedObject( self, TextColorKey, color, OBJC_ASSOCIATION_RETAIN_NONATOMIC ); } - (UIColor *)floatingLabelTextColor { return objc_getAssociatedObject(self, TextColorKey); } -
extension UITextField { var floatingLabelYPadding: CGFloat { set { objc_setAssociatedObject(
self, &YPaddingKey, newValue, objc_AssociationPolicy(OBJC_ASSOCIATION_ASSIGN) ) } get { if let padding = objc_getAssociatedObject( self, &YPaddingKey) as? CGFloat { return padding } else { return 0 -
extension UITextField { var floatingLabelYPadding: CGFloat { set { objc_setAssociatedObject(
self, &YPaddingKey, newValue, objc_AssociationPolicy(OBJC_ASSOCIATION_ASSIGN) ) } get { if let padding = objc_getAssociatedObject( self, &YPaddingKey) as? CGFloat { return padding } else { return 0 -
&TextColorKey, newValue, objc_AssociationPolicy( OBJC_ASSOCIATION_RETAIN_NONATOMIC) ) } get { if let
color = objc_getAssociatedObject( self, &TextColorKey) as? UIColor { return color } else { return UIColor.blackColor() } } } } -
// objc4-532/runtime/objc-references.mm // class AssociationsManager manages a lock /
hash table // singleton pair. Allocating an instance acquires the // lock, and calling its assocations() method // lazily allocates it. class AssociationsManager { static OSSpinLock _lock; // associative references: // object pointer -> PtrPtrHashMap. static AssociationsHashMap *_map; public: AssociationsManager() { OSSpinLockLock(&_lock); } ~AssociationsManager() { OSSpinLockUnlock(&_lock); } }; -
CGFloat *floatingLabelYPadding UIFont *floatingLabelFont UIColor *floatingLabelTextColor UIColor *floatingLabelActiveColor BOOL animateEvenIfNotFirstResponder
JVFloatLabeledOptions UITextField (JVFloatLabeledTextField) JVFloatLabeledOptions *options -
extension UITextField { var options: FloatLabelOptions { get { if
let options = objc_getAssociatedObject( self, &OptionsKey) as? FloatLabelOptions { return options } else { return FloatLabelOptions() } } set { // ... } } } -
extension UITextField { var options: FloatLabelOptions { get { if
let options = objc_getAssociatedObject( self, &OptionsKey) as? FloatLabelOptions { return options } else { return FloatLabelOptions() } } set { // ... } } } -
extension UITextField { var options: FloatLabelOptions { get { if
let options = objc_getAssociatedObject( self, &OptionsKey) as? FloatLabelOptions { return options } else { return FloatLabelOptions() } } set { // ... } } } -
extension UITextField { var options: FloatLabelOptions { get { if
let options = objc_getAssociatedObject( self, &OptionsKey) as? FloatLabelOptions { return options } else { return FloatLabelOptions() } } set { // ... } } } -
@objc class FloatLabelOptions { let yPadding: CGFloat let font: UIFont
let textColor: UIColor // ... } -
@objc class FloatLabelOptions { let yPadding: CGFloat let font: UIFont
let textColor: UIColor // ... } -
let options = MDCSwipeOptions() options.threshold = 130 options.onPan = {
state in switch state.direction { case .Left: self.bookmarkView.alpha = 0 self.dontBookmarkView.alpha = state.thresholdRatio case .Right: self.bookmarkView.alpha = state.thresholdRatio self.dontBookmarkView.alpha = 0 } } webView.mdc_swipeToChooseSetup(options) -
let options = MDCSwipeOptions() options.threshold = 130 options.onPan = {
state in switch state.direction { case .Left: self.bookmarkView.alpha = 0 self.dontBookmarkView.alpha = state.thresholdRatio case .Right: self.bookmarkView.alpha = state.thresholdRatio self.dontBookmarkView.alpha = 0 } } webView.mdc_swipeToChooseSetup(options) -
let options = MDCSwipeOptions() options.threshold = 130 options.onPan = {
state in switch state.direction { case .Left: self.bookmarkView.alpha = 0 self.dontBookmarkView.alpha = state.thresholdRatio case .Right: self.bookmarkView.alpha = state.thresholdRatio self.dontBookmarkView.alpha = 0 } } webView.mdc_swipeToChooseSetup(options) -
let options = MDCSwipeOptions() options.threshold = 130 options.onPan = {
state in switch state.direction { case .Left: self.bookmarkView.alpha = 0 self.dontBookmarkView.alpha = state.thresholdRatio case .Right: self.bookmarkView.alpha = state.thresholdRatio self.dontBookmarkView.alpha = 0 } } webView.mdc_swipeToChooseSetup(options) -
let options = MDCSwipeOptions() options.threshold = 130 options.onPan = {
state in switch state.direction { case .Left: self.bookmarkView.alpha = 0 self.dontBookmarkView.alpha = state.thresholdRatio case .Right: self.bookmarkView.alpha = state.thresholdRatio self.dontBookmarkView.alpha = 0 } } webView.mdc_swipeToChooseSetup(options) -
let options = MDCSwipeOptions() options.threshold = 130 options.onPan = {
state in switch state.direction { case .Left: self.bookmarkView.alpha = 0 self.dontBookmarkView.alpha = state.thresholdRatio case .Right: self.bookmarkView.alpha = state.thresholdRatio self.dontBookmarkView.alpha = 0 } } webView.mdc_swipeToChooseSetup(options) -
let options = MDCSwipeOptions() options.threshold = 130 options.onPan = {
state in switch state.direction { case .Left: self.bookmarkView.alpha = 0 self.dontBookmarkView.alpha = state.thresholdRatio case .Right: self.bookmarkView.alpha = state.thresholdRatio self.dontBookmarkView.alpha = 0 } } webView.mdc_swipeToChooseSetup(options) -
let options = MDCSwipeOptions() options.threshold = 130 options.onPan = {
state in switch state.direction { case .Left: self.bookmarkView.alpha = 0 self.dontBookmarkView.alpha = state.thresholdRatio case .Right: self.bookmarkView.alpha = state.thresholdRatio self.dontBookmarkView.alpha = 0 } } webView.mdc_swipeToChooseSetup(options) -
let options = MDCSwipeOptions() options.threshold = 130 options.onPan = {
state in switch state.direction { case .Left: self.bookmarkView.alpha = 0 self.dontBookmarkView.alpha = state.thresholdRatio case .Right: self.bookmarkView.alpha = state.thresholdRatio self.dontBookmarkView.alpha = 0 } } webView.mdc_swipeToChooseSetup(options) -
public struct ButtonCallbackParameters { let buttonIndex: Int let buttonTitle: String
let totalButtonCount: Int } public typealias ButtonCallback = (parameters: ButtonCallbackParameters) -> () -
public struct ButtonCallbackParameters { let buttonIndex: Int let buttonTitle: String
let totalButtonCount: Int } public typealias ButtonCallback = (parameters: ButtonCallbackParameters) -> () @availability(*, deprecated=2.0) -
extension UITextField { var options: FloatLabelOptions { get { //
... } set { // ... } } } -
let options = MDCSwipeOptions() options.threshold = 130 options.onPan = {
state in // ... } webView.mdc_swipeToChooseSetup(options) -
let options = MDCSwipeOptions() options.threshold = 130 options.onPan = {
state in // ... } webView.mdc_swipeToChooseSetup(options)