没有使用返回值时, 警告
swift:
@warn_unused_result func doSomething() -> Bool {
return true
}
OC:
- (BOOL)doSomething __attribute__((warn_unused_result)) {
return YES;
}
没有使用返回值时, 不警告
swift:
@discardableResult func doSomething() -> Bool {
return true
}
OC:
To prevent the compiler from flooding us with warnings when importing Objective-C code the @discardableResult attribute is automatically added for all non-void functions that are not marked with the ((warn_unused_result)) attribute.
本文探讨了在Swift和Objective-C编程语言中如何处理未使用的返回值可能导致的警告。介绍了@warn_unused_result和@discardableResult属性在Swift中的使用,以及Objective-C中相应的属性,帮助开发者避免编译器警告,提升代码质量。

373

被折叠的 条评论
为什么被折叠?



