Giter Site home page Giter Site logo

nullsafe's Introduction

Build Status

Purpose

NullSafe is a simple category on NSNull that returns nil for unrecognised messages instead of throwing an exception. This eliminates a common cause of crashes, where (for example) JSON data contains a null value instead of an array or string, and the network code in the app isn't expecting it.

Supported iOS & SDK Versions

  • Supported build target - iOS 12.0 / Mac OS 10.14 (Xcode 10.1)
  • Earliest supported deployment target - iOS 9.0 / Mac OS 10.10
  • Earliest compatible deployment target - iOS 4.3 / Mac OS 10.6

NOTE: 'Supported' means that the library has been tested with this version. 'Compatible' means that the library should work on this iOS version (i.e. it doesn't rely on any unavailable SDK features) but is no longer being tested for compatibility and may require tweaking or bug fixes to run correctly.

ARC Compatibility

NullSafe works in both ARC and non ARC targets without modification.

Installation & Usage

To use NullSafe, just drag the NullSafe.m file into your project. NullSafe will be automatically loaded at runtime, you don't need to include any header files in your code.

Selectively Disabling NullSafe

NullSafe is enabled automatically as soon as the class files are added to a project target, however if you wish to disable NullSafe for a particular scheme (e.g. when running in debug mode) then you can disable it by adding the following pre-compiler macro to your build settings:

NULLSAFE_ENABLED=0

Or if you prefer, you could add something like this to your prefix.pch file:

#ifdef DEBUG
#define NULLSAFE_ENABLED 0
#endif

Release Notes

Version 2.0

  • Radically simplified NullSafe to only handle JSON and Plist values

Version 1.2.3

  • Updated for Xcode 9
  • Fixed thread safety issue
  • Added tvOS support to podspec

Version 1.2.2

  • Fixed nullability warning

Version 1.2.1

  • Fixed crash in iOS 8
  • Removed NullSafe.h file (redundant)
  • Now complies with -Weverything warning level

Version 1.2

  • Will now handle any known method, not just a subset
  • Added NULLSAFE_ENABLED macro to easily enable/disable per scheme
  • Added implementation cache for better performance

Version 1.1

  • Made it work by design rather than coincidence ;-)

Version 1.0

  • Initial release

nullsafe's People

Contributors

nicklockwood avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nullsafe's Issues

How to get it work?

I imported to my project and if I try to set a value to NSNULL class in NSDictionary it crashes.

How to make it work so that it handles all null pointer exemptions.

Private methods

I noticed that installing NullSafe (with Cocoapods) made the following being logged when the app runs:

CLTilesManagerClient: initialize, sSharedTilesManagerClient
CLTilesManagerClient: init
CLTilesManagerClient: reconnecting, 0x7fa1c5288290

This looks like a private Apple framework. Can I ship my app with this?

Is it necessary to look up method signature?

Because in the forwardInvocation method, set the target of the invocation to nil, so I think,whatever the methodSignature, it doesn't matter. why not just return "v@:" , the simplest methodSignature.

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector { NSMethodSignature *signature = [super methodSignatureForSelector:aSelector]; if (!signature) { signature = [NSMethodSignature signatureWithObjCTypes:"v@:"]; } return signature; }

Xcode 7 build warning

When compiling NullSafe in Xcode 7 I get the following warning:

NullSafe.m:128:17: warning: null passed to a callee that requires a non-null argument [-Wnonnull] [invocation invokeWithTarget:nil];

It seems like we might be able to remove this call and it resolves the issue (and still works in my tests)

why not creat a method signature directly to return?

why not creat a valid method signature to return in - (NSMethodSignature *)methodSignatureForSelector:(SEL)selector like this NSString *str = @"123"; NSMethodSignature *sign = [str methodSignatureForSelector:@selector(length)]; return sign; ? because in method - (void)forwardInvocation:(NSInvocation *)invocation you set target to be nil object, so the method signature of invocation is not important.

Nullsafe will crash when using in background thread.

We use the latest Nullsafe code in our product and find crashes like this:
SIGSEGV SEGV_ACCERR "-[NSNull(NullSafe) methodSignatureForSelector:] (NullSafe.m:81)"

The reason is that Nullsafe will find all the classes in the system and initialize one instance.
Some classes like UIWebView cannot initialize in background thread thus caused the crash.
A demo code is:
// This code will crash
dispatch_async(dispatch_get_global_queue(0, 0), ^{
UIWebView *v1 = [[UIWebView alloc] init];
[v1 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]];
});

Nullsafe's idea is great and we enjoy using it. Can you fix this in the coming version?

  • (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
    {
    @synchronized([self class])
    {
    dispatch_async(dispatch_get_main_queue(), ^{
    //push the code here may help.
    });
    }
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.