程序开发那些开发的事寒哥管理的技术专题

关于XCode7你以为只有Swift (objc语法增强)

2015-06-11  本文已影响2031人  南栀倾寒

写在开头 iOS开发者 群532084214 给大家提供一个交流技术 也可以聊天打屁的平台

目前只支持NSArray NSSet NSDictionary

 NSArray<UIImage *> *images;
 NSDictionary<NSString *, NSURL *> *resourcesByName;

泛型和id类型配合(kind of)
在Objc中 我们知道id是万能指针 可以呼出来任何继承NSObject的方法 配合泛型使用就是可以呼出来泛型的父类的所有方法

-[UIView subviewWithTag:]取出来是 个 UIView*
可以直接调用UIView的方法

 UIButton *button = [view subviewWithTag:0]; // okay: UIButton is a UIView
[[view subviewWithTag:0] setTitle:@"Bounded" forState:  UIControlStateNormal]; //
okay: method found in UIButton
UIResponder *responder = [view subviewWithTag:0]; 
 // okay:      UIView is a UIResponder
 NSString *string = [view subviewWithTag:0]; 
// error: UIView is unrelated to NSString

    typedef NS_ENUM(NSInteger, DisplayMode) {
   DisplayMode256Colors NS_SWIFT_NAME(With256Colors),
   DisplayModeThousandsOfColors,
   DisplayModeMillionsOfColors
 };

导入到Swift就是

 @objc enum DisplayMode : Int {
   case With256Colors
   case ThousandsOfColors
   case MillionsOfColors
}

再比如

@interface MyController : UIViewController
 + (instancetype)standardControllerForURLKind:(URLKind)kind
 NS_SWIFT_NAME(init(URLKind:));
 @end

到swift就是

   class MyController : UIViewController {
   init(URLKind kind: URLKind)
 }
上一篇 下一篇

猜你喜欢

热点阅读