NSDictionary<NSString *, MyModel *> *dic = nil;
// - 如果没有 kindof 需要进行强转
@property (nonatomic,readonly,copy) NSArray< UIView *> *subviews;
- (nullable UIView *)viewWithTag:(NSInteger)tag;
UIButton *button = (UIButton *)[self.view viewWithTag:1000];
// - 有 kindof 写法 不需要强转
@property (nonatomic,readonly,copy) NSArray<__kindof UIView *> *subviews;
- (nullable __kindof UIView *)viewWithTag:(NSInteger)tag;
UIButton *button = [self.view viewWithTag:1000];
- (instancetype)dictionaryWithObjects:(const ObjectType [])objects forKeys:(const KeyType [])keys count:(NSUInteger)cnt;
- NS_NOESCAPE 用于修饰 block.表示block返回之前调用,该修饰符可以让编译器做很多优化,比如剔除对 self 的捕获、持有、释放等。
- (void)enumerateKeysAndObjectsUsingBlock:(void (NS_NOESCAPE ^)(KeyType key, ObjectType obj, BOOL *stop))block;
- (void)enumerateObjectsUsingBlock:(void (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
- (void)runBlock:(NS_NOESCAPE dispatch_block_t)block {
block();
}
- NS_REQUIRES_NIL_TERMINATION 要求最后一个值为 nil。
- (instancetype)initWithObjectsAndKeys:(id)firstObject, ... NS_REQUIRES_NIL_TERMINATION;
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"1", nil];
// - 使用
- (void)testParameters:(id)first, ... {
if (!first) return;
va_list list;
va_start(list, first);
// 遍历,va_arg 读取对象,并将指针指向下一个对象
for (id obj = first; obj; obj = va_arg(list, id)) {
NSLog(@"%@", obj);
}
va_end(list);
}
- NS_REQUIRES_SUPER 要求当前方法必须调用 super。如果在子类中未调用 super,则编译警告。
- (void)updateConstraints NS_REQUIRES_SUPER;
typedef NS_ENUM(NSInteger,SearchState) {
SearchStateNotSearch,
SearchStateSearching,
SearchStateSearchFinished,
SearchStateSearchFailed
};
typedef NS_OPTIONS(NSUInteger, NSKeyValueObservingOptions) {
NSKeyValueObservingOptionNew,
NSKeyValueObservingOptionOld,
NSKeyValueObservingOptionInitial,
NSKeyValueObservingOptionPrior
};
// - 类型声明
typedef NSString * QIEComponentNameOption NS_EXTENSIBLE_STRING_ENUM;
// - 字符串定义
FOUNDATION_EXPORT QIEComponentNameOption const QIEComponentNameConfigComponet;
// - 字符串赋值
QIEComponentNameOption const QIEComponentNameConfigComponet = @"QIEConfigComponent";
// - 声明
extern NSString *const SearchErrorDomain;
extern NSInteger SearchDefaultTimeout;
// - 实现
NSString *const SearchErrorDomain = @"SearchErrorDomain";
const NSInteger SearchDefaultTimeout = 20;
- designated initializer 标明必须用这个方法初始化 指明初始化方法 可以保证所有变量可以被正确的初始化
@interface WKWebView : UIView
- (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER;
@end
- available 声明本接口最低支持的操作系统版本
// - iOS10 开始提供了新的 avaiable 宏 API_AVAILABLE 分别是
API_AVAILABLE(macos(10.10))
API_AVAILABLE(macos(10.9), ios(10.0))
API_AVAILABLE(macos(10.4), ios(8.0), watchos(2.0), tvos(10.0))
// - 使用
typedef NS_ENUM(NSInteger,SearchState) {
SearchStateNotSearch,
SearchStateSearching,
SearchStateSearchFinished,
SearchStateSearchFailed
} NS_ENUM_AVAILABLE_IOS(2_0);//此枚举在iOS2.0以上才能使用
NS_CLASS_AVAILABLE_IOS(2_0) //此类在iOS2.0以上才能使用
@interface SearchManager : NSObject
- (void)reSearch NS_AVAILABLE_IOS(5_0);//此方法在iOS5.0以上才能使用
@end
- unavailable 声明此接口不可用,大多数时候是用于声明所在平台限制
// - iOS10开始提供了新的unavailable宏API_UNAVAILABLE:
API_UNAVAILABLE(macos)
API_UNAVAILABLE(watchos, tvos)
// - 使用
@interface SearchManager : NSObject
- (void)searchInWatch NS_UNAVAILABLE;//不能用此接口
- (void)searchInHostApp NS_EXTENSION_UNAVAILABLE_IOS;//extension里不能用此接口
- (void)search __TVOS_PROHIBITED;//tvOS里不能用此接口,可修饰枚举,类,方法,参数
@end
- deprecated 声明此接口已经被弃用,可以同时加注释注明替代接口
// - iOS10开始提供了新的deprecated宏API_DEPRECATED和API_DEPRECATED_WITH_REPLACEMENT。前者可以注明弃用原因,后者可以注明替代接口。
API_DEPRECATED("No longer supported", macos(10.4, 10.8))
API_DEPRECATED("No longer supported", macos(10.4, 10.8), ios(2.0, 3.0), watchos(2.0, 3.0), tvos(9.0, 10.0))
API_DEPRECATED_WITH_REPLACEMENT("-setName:", tvos(10.0, 10.4), ios(9.0, 10.0))
API_DEPRECATED_WITH_REPLACEMENT("SomeClassName", macos(10.4, 10.6), watchos(2.0, 3.0))
// - 注明废弃类
NS_CLASS_DEPRECATED_IOS(2_0, 9_0, "UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead")
@interface UIAlertView : UIView
@end
// - 注明废弃API
@interface UIViewController : UIResponder
- (void)viewDidUnload NS_DEPRECATED_IOS(3_0,6_0);
@end
// - 注明废弃枚举
typedef NS_ENUM(NSInteger, UIStatusBarStyle) {
UIStatusBarStyleDefault = 0, // Dark content, for use on light backgrounds
UIStatusBarStyleLightContent NS_ENUM_AVAILABLE_IOS(7_0) = 1, // Light content, for use on dark backgrounds
UIStatusBarStyleBlackTranslucent NS_ENUM_DEPRECATED_IOS(2_0, 7_0, "Use UIStatusBarStyleLightContent") = 1,
UIStatusBarStyleBlackOpaque NS_ENUM_DEPRECATED_IOS(2_0, 7_0, "Use UIStatusBarStyleLightContent") = 2,
}
打印宏
// - 定义
#define QIELog(frmt, ...) [[QIELogManager shareInstants] logWithFile:__FILE__ function:__func__ line:__LINE__ format:(frmt), ## __VA_ARGS__]
// - 实现
- (void)logWithFile:(const char *)file function:(const char *)funcation line:(NSUInteger)line format:(NSString *)format, ... {
if (!self.enableLog) {
return;
}
NSString *filePath = [NSString stringWithUTF8String:file];
for (QIEComponentNameOption option in self.componentArray) {
if ([filePath containsString:option]) {
return;
}
}
NSString *componentName = @"";
NSArray *fileComponent = [filePath componentsSeparatedByString:@"/"];
if ([fileComponent containsObject:@"Pods"]) {
NSInteger index = [fileComponent indexOfObject:@"Pods"];
if (fileComponent.count > index + 1) {
componentName = [NSString stringWithFormat:@"【%@】",fileComponent[index + 1]];
}
}
if (format) {
va_list args;
va_start(args, format);
NSString *message = [[NSString alloc] initWithFormat:format arguments:args];
va_end(args);
fprintf(stderr, "%s<%s : %lu> %s\n",
[componentName UTF8String],
[[filePath lastPathComponent] UTF8String],
(unsigned long)line, funcation);
NSLog(@"%@", message);
fprintf(stderr, "\n");
}
}