NitroMisc 1.1.0

NitroMisc 1.1.0

测试测试版
语言语言 Obj-CObjective C
许可证 MIT
发布最后发布2014 年 12 月

Daniel L. Alves 维护。



NitroMisc 1.1.0

NitroMisc 除了提供很多有用的实用宏外,还提供了许多 iOS 基础和 UIKit 类中缺失的代码。您将会发现:

  • NSBundle 实用程序
  • 更短的 NSError 实例化方式
  • 更短的 NSInvocation 实例化方式
  • 日志宏
  • 单例宏
  • 禁用无用的警告宏

示例

1) NSBundle 实用程序

所有应用任务都在一个地方执行

// Returns the value associated with the `kCFBundleNameKey` key in the main bundle information 
// property list
NSString *myAppName = [NSBundle applicationName];

// Returns the value associated with the `kCFBundleVersionKey` key in the main bundle 
// information property list
NSString *myAppVersion = [NSBundle applicationVersion];

// Reads a plist as a `NSDictionary`
NSError *error = nil;
[anyNSBundleObject readPropertyListWithName: @"myPlistNameWithOrWithoutExtension"
                                      error: &error];

// Reads any file as a `NSString`
NSString *fileContent = [anyNSBundleObject stringWithEncoding: NSUTF8StringEncoding
                                         fromResourceWithName: @"yourFileName"
                                                         type: @"yourFileExtension"];

2) 更短的 NSError 实例化方式

90% 的时间只需设置 localizedDescription 键?这就对了

NSError *muchBetterError = [NSError errorWithDomain: kMyAppNSErrorDomain
                                               code: kMyAppShouldNotHappenErrorCode
                               localizedDescription: @"A localized description"];

3) 更短的 NSInvocation 实例化方式

为了创建 NSInvocation 对象而编写样板代码?请提供

NSInvocation *niceInvocation = [NSInvocation invocationForSelector: @selector( invocationSelector )
                                                        withTarget: self];

4) 日志宏

这些宏使用 NSLog 将通用消息记录到 Apple 系统日志设施。当 DEBUG 预处理器宏未定义或为假时,对这些宏的调用将从您的代码中移除,这不会产生编译、链接或二进制开销。此外,它们还提供了将日志消息与调用它们的函数名一起显示的额外功能。

// These will vanish if the DEBUG preprocessor macro is false
// or is not defined
NTR_LOG( @"%@ Simpson", @"Margie" ); // Logs "Margie Simpson"
NTR_LOGI( @"%@ Simpson", @"Liza" );  // Logs "INFO: Liza Simpson"
NTR_LOGW( @"%@ Simpson", @"Bart" );  // Logs "WARNING: Bart Simpson"
NTR_LOGE( @"%@ Simpson", @"Homer" ); // Logs "ERROR: Homer Simpson"

5) 单例宏

不再需要在您的项目中四处复制粘贴单例生成宏?但 NitroMisc 可以满足您的所有需求,并且为 ARC 代码进行了优化。

// .h file
@interface SingletonClass : NSObject
DEFAULT_DECLARE_SINGLETON_FOR_CLASS( SingletonClass )
@end

// .m file
@implementation SingletonClass
DEFAULT_SYNTHESIZE_SINGLETON_FOR_CLASS( SingletonClass )
@end

// Elsewhere: access the singleton
SingletonClass.sharedInstance

不喜欢 sharedInstance 访问器名称?没问题

// .h file
@interface FancySingletonClass : NSObject
DECLARE_SINGLETON_FOR_CLASS( FancySingletonClass, theOne )
@end

// .m file
@implementation FancySingletonClass
SYNTHESIZE_SINGLETON_FOR_CLASS( FancySingletonClass, theOne )
@end

// Elsewhere: access the singleton
FancySingletonClass.theOne

6) 禁用无用的警告宏

使用 SuppressPerformSelectorLeakWarning 宏来禁用“performSelector 的 selector 未知可能会造成内存泄漏”的警告。请记住:这应该只在您确定对象响应选择器时使用。

-( void )letsFireASelectorWhoseNameWeDontKnow:( SEL )aSelector
{
    // This code generates no warnings. But if you remove the macro...
    if( [object respondsToSelector: aSelector] )
        SuppressPerformSelectorLeakWarning( [object performSelector: aSelector] );
}

要求

iOS 4.3 或更高版本,仅适用于 ARC

安装

NitroMisc 通过 CocoaPods 提供,安装它只需将以下行添加到您的 Podfile 中

pod "NitroMisc"

NitroMisc 会将 -ObjC 连接器标志添加到使用它的目标中。没有它,分类代码将被删除,导致链接错误。有关在静态库中分类的更多信息,请参阅:[在静态库中使用分类构建 Objective-C](https://developer.apple.com/library/mac/qa/qa1490/_index.html)

作者

许可证

NitroMisc 在 MIT 许可证下提供。有关更多信息,请参阅 LICENSE 文件。