NSString and NSDictionaryForKey value - nil?

How can I put the default value in my string if I get data from a blank key / value in a dictionary.

so [myObject setMyString:[dictionary valueForKey:@"myKey"]];

So, if I did NSString *newString = myObject.myString, I would receive unrecognized selector error.

So again, I just need a way to insert a default row if the key is empty;

+5
source share
8 answers

How about this one?

NSString *value = [dictionary valueForKey:@"myKey"];
if (!value) value = @"defaultValue";
[myObject setMyString:value];
+9
source

dictionary NSDictionary, , , objectForKey, valueForKey KVC. NSDictionary, , NSDictionary KVC, . "@allKeys" "@count".

, , , :

[dictionary objectForkey:@"myKey"] ?: @"defaultValue"

get , - - ...

[[dictionary objectsForKeys:[NSArray arrayWithObject:@"myKey"]
             notFoundMarker:@"defaultValue"]
 objectAtIndex:0]

:)

+10

[NSNull null] , .

" "; newString nil ( [NSNull null], , - - , NSUserDefaults?

+5

NSDictionary.

NSDictionary + NSDictionaryExtensions.h

#import <Foundation/Foundation.h>

@interface NSDictionary (NSDictionaryExtensions)

- (id)objectForKey:(id)aKey defaultObject: (id) defObj;

@end

NSDictionary + NSDictionaryExtensions.m

#import "NSDictionary+NSDictionaryExtensions.h"

@implementation NSDictionary (NSDictionaryExtensions)

- (id)objectForKey:(id)aKey defaultObject: (id) defObj
{
    id ret = [self objectForKey: aKey];
    if ( ret == nil )
        return defObj;
    else
        return ret;
}

@end

:

NSString* str = [dict objectForKey: @"a_key" defaultObject: @"default"];
+2

if ( myObject == nil ) {
   [myObject setMyString:@""];
}

if ( [dictionary valueForKey:@"myKey"] == nil ) {
    [dictionary setObject:@"" forKey:@"myKey"];
}
0

, , myObject.myString " " - myString , ( "", Objective-C , ( -) , !)

? - myObject.myString, .

, valueForKey; nil, , :

- (void) setMyString:(NSString *)value
{  myString = value;
}

- (NSString *) myString
{
   if (myString == nil)
      @throw [NSException exceptionWithName:@"UnrecognizedSelector" reason:@"No value set for myString" userInfo:nil];
   return myString;
}

:

  • , ,

  • /ARC, //

? , , - .

0

@Paul Lynch [NSNull null] nil. , :

- (id) objectForKey: (id) key withDefault: (id) defaultValue
{
    id value = self[key];

    if (value == nil || [value isEqual: [NSNull null]] || ![defaultValue isKindOfClass: [value class]])
    {
        value = defaultValue;
    }

    return value;
}
0

nil, :

extension NSDictionary {

func convertToString() -> String {
    let jsonData = try! JSONSerialization.data(withJSONObject: self, options: JSONSerialization.WritingOptions.prettyPrinted) as NSData!

    if jsonData  == nil{
        return "{}"
    }else {
        return String(data: jsonData as! Data, encoding: String.Encoding.utf8)!
    }
}

func object_forKeyWithValidationForClass_Int(aKey: String) -> Int {

    // CHECK FOR EMPTY
    if(self.allKeys.count == 0) {
        return Int()
    }

    // CHECK IF KEY EXIST
    if let val = self.object(forKey: aKey) {
        if((val as AnyObject).isEqual(NSNull())) {
            return Int()
        }
    } else {
        // KEY NOT FOUND
        return Int()
    }

    // CHECK FOR NIL VALUE
    let aValue : AnyObject = self.object(forKey: aKey)! as AnyObject
    if aValue.isEqual(NSNull()) {
        return Int()
    }
    else if(aValue.isKind(of: NSString.self)){
        return Int((aValue as! NSString).intValue)
    }
    else {

        if aValue is Int {
            return self.object(forKey: aKey) as! Int
        }
        else{
            return Int()
        }
    }
}

func object_forKeyWithValidationForClass_CGFloat(aKey: String) -> CGFloat {

    // CHECK FOR EMPTY
    if(self.allKeys.count == 0) {
        return CGFloat()
    }

    // CHECK IF KEY EXIST
    if let val = self.object(forKey: aKey) {
        if((val as AnyObject).isEqual(NSNull())) {
            return CGFloat()
        }
    } else {
        // KEY NOT FOUND
        return CGFloat()
    }

    // CHECK FOR NIL VALUE
    let aValue : AnyObject = self.object(forKey: aKey)! as AnyObject
    if aValue.isEqual(NSNull()) {
        return CGFloat()
    }
    else {

        if aValue is CGFloat {
            return self.object(forKey: aKey) as! CGFloat
        }
        else{
            return CGFloat()
        }
    }
}

func object_forKeyWithValidationForClass_String(aKey: String) -> String {

    // CHECK FOR EMPTY
    if(self.allKeys.count == 0) {
        return String()
    }

    // CHECK IF KEY EXIST
    if let val = self.object(forKey: aKey) {
        if((val as AnyObject).isEqual(NSNull())) {
            return String()
        }
    } else {
        // KEY NOT FOUND
        return String()
    }

    // CHECK FOR NIL VALUE
    let aValue : AnyObject = self.object(forKey: aKey)! as AnyObject
    if aValue.isEqual(NSNull()) {
        return String()
    }
    else if(aValue.isKind(of: NSNumber.self)){
        return String(format:"%f", (aValue as! NSNumber).doubleValue)
    }
    else {

        if aValue is String {
            return self.object(forKey: aKey) as! String
        }
        else{
            return String()
        }
    }
}

func object_forKeyWithValidationForClass_StringInt(aKey: String) -> String {

    // CHECK FOR EMPTY
    if(self.allKeys.count == 0) {
        return String()
    }

    // CHECK IF KEY EXIST
    if let val = self.object(forKey: aKey) {
        if((val as AnyObject).isEqual(NSNull())) {
            return String()
        }
    } else {
        // KEY NOT FOUND
        return String()
    }

    // CHECK FOR NIL VALUE
    let aValue : AnyObject = self.object(forKey: aKey)! as AnyObject
    if aValue.isEqual(NSNull()) {
        return String()
    }
    else if(aValue.isKind(of: NSNumber.self)){
        return String(format:"%d", (aValue as! NSNumber).int64Value)


    }
    else {

        if aValue is String {
            return self.object(forKey: aKey) as! String
        }
        else{
            return String()
        }
    }
}

func object_forKeyWithValidationForClass_Bool(aKey: String) -> Bool {
    // CHECK FOR EMPTY
    if(self.allKeys.count == 0) {
        return Bool()
    }

    // CHECK IF KEY EXIST
    if let val = self.object(forKey: aKey) {
        if((val as AnyObject).isEqual(NSNull())) {
            return Bool()
        }
    } else {
        // KEY NOT FOUND
        return Bool()
    }

    // CHECK FOR NIL VALUE
    let aValue : AnyObject = self.object(forKey: aKey)! as AnyObject
    if aValue.isEqual(NSNull()) {
        return Bool()
    }
    else {

        if aValue is Bool {
            return self.object(forKey: aKey) as! Bool
        }
        else{
            return Bool()
        }
    }
}

func object_forKeyWithValidationForClass_NSArray(aKey: String) -> NSArray {
    // CHECK FOR EMPTY
    if(self.allKeys.count == 0) {
        return NSArray()
    }

    // CHECK IF KEY EXIST
    if let val = self.object(forKey: aKey) {
        if((val as AnyObject).isEqual(NSNull())) {
            return NSArray()
        }
    } else {
        // KEY NOT FOUND
        return NSArray()
    }

    // CHECK FOR NIL VALUE
    let aValue : AnyObject = self.object(forKey: aKey)! as AnyObject
    if aValue.isEqual(NSNull()) {
        return NSArray()
    }
    else {
        if aValue is NSArray {
            return self.object(forKey: aKey) as! NSArray
        }
        else{
            return NSArray()
        }
    }
}

func object_forKeyWithValidationForClass_NSMutableArray(aKey: String) -> NSMutableArray {
    // CHECK FOR EMPTY
    if(self.allKeys.count == 0) {
        return NSMutableArray()
    }

    // CHECK IF KEY EXIST
    if let val = self.object(forKey: aKey) {
        if((val as AnyObject).isEqual(NSNull())) {
            return NSMutableArray()
        }
    } else {
        // KEY NOT FOUND
        return NSMutableArray()
    }

    // CHECK FOR NIL VALUE
    let aValue : AnyObject = self.object(forKey: aKey)! as AnyObject
    if aValue.isEqual(NSNull()) {
        return NSMutableArray()
    }
    else {

        if aValue is NSMutableArray {
            return self.object(forKey: aKey) as! NSMutableArray
        }
        else{
            return NSMutableArray()
        }
    }
}

func object_forKeyWithValidationForClass_NSDictionary(aKey: String) -> NSDictionary {
    // CHECK FOR EMPTY
    if(self.allKeys.count == 0) {
        return NSDictionary()
    }

    // CHECK IF KEY EXIST
    if let val = self.object(forKey: aKey) {
        if((val as AnyObject).isEqual(NSNull())) {
            return NSDictionary()
        }
    } else {
        // KEY NOT FOUND
        return NSDictionary()
    }

    // CHECK FOR NIL VALUE
    let aValue : AnyObject = self.object(forKey: aKey)! as AnyObject
    if aValue.isEqual(NSNull()) {
        return NSDictionary()
    }
    else {

        if aValue is NSDictionary {
            return self.object(forKey: aKey) as! NSDictionary
        }
        else{
            return NSDictionary()
        }
    }
}

func object_forKeyWithValidationForClass_NSMutableDictionary(aKey: String) -> NSMutableDictionary {
    // CHECK FOR EMPTY
    if(self.allKeys.count == 0) {
        return NSMutableDictionary()
    }

    // CHECK IF KEY EXIST
    if let val = self.object(forKey: aKey) {
        if((val as AnyObject).isEqual(NSNull())) {
            return NSMutableDictionary()
        }
    } else {
        // KEY NOT FOUND
        return NSMutableDictionary()
    }

    // CHECK FOR NIL VALUE
    let aValue : AnyObject = self.object(forKey: aKey)! as AnyObject
    if aValue.isEqual(NSNull()) {
        return NSMutableDictionary()
    }
    else {
        if aValue is NSMutableDictionary {
            return self.object(forKey: aKey) as! NSMutableDictionary
        }
        else{
            return NSMutableDictionary()
        }
    }
}

}

0
source

All Articles