寫在開頭 iOS開發(fā)者 群532084214 給大家提供一個交流技術(shù) 也可以聊天打屁的平臺
- 千呼萬喚始出來的泛型語法
目前只支持NSArray NSSet NSDictionary
NSArray<UIImage *> *images;
NSDictionary<NSString *, NSURL *> *resourcesByName;
泛型和id類型配合(kind of)
在Objc中 我們知道id是萬能指針 可以呼出來任何繼承NSObject的方法 配合泛型使用就是可以呼出來泛型的父類的所有方法
如 -[UIView subviewWithTag:]
取出來是 個 UIView*
可以直接調(diào)用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
- NS_SWIFT_NAME宏 可以在混編時導入到Swift自定義名稱
如
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)
}