Swift Equivalent to '[Nsdictionary Initwithobjects: Forkeys:]'

Swift equivalent to `[NSDictionary initWithObjects: forKeys:]`

As of Swift 4 you can create a dictionary directly from a
sequence of key/value pairs:

let keys = ["one", "two", "three"]
let values = [1, 2, 3]

let dict = Dictionary(uniqueKeysWithValues: zip(keys, values))

print(dict) // ["one": 1, "three": 3, "two": 2]

This assumes that all keys are different, otherwise it will abort
with a runtime exception.

If the keys are not guaranteed to be distinct then you can do

let keys = ["one", "two", "one"]
let values = [1, 2, 3]

let dict = Dictionary(zip(keys, values), uniquingKeysWith: { $1 })

print(dict) // ["one": 3, "two": 2]

The second argument is a closure which determines which value "wins"
in the case of duplicate keys.

the exact replacement for dictionaryWithObjects: forKeys: in swift

You should be able to update dictionary directly even the value is object

var eachKey = alphabets[i] as! String
in_memory_prediction[eachKey] = prediction[i]!

IOS: [NSDictionary initWithObjects:forKeys:]:

Its due to NULL value.

Here is your code which crash your app:

 NSArray *keys = [NSArray arrayWithObjects: @"api_key", @"truck_id",@"driver_id", @"member_id",@"amount",@"net_amount",@"vendor_id",@"delivery_date",@"address_id", @"create_Id", @"transaction_id",@"Cart_Details", nil];

// @"DriverIdStr"
NSArray *objects = [NSArray arrayWithObjects:@"bf45c093e542f057c123ae7d6",@"TruckIdStr",NULL,@"Member_id",@"amount",@"GrandTotalString",@"vendor_id",@"delivery_date",@"RcvdAddressId",@"create_Id",@"Transaction_Id",@"SelectedItemsArray", nil];

If I give NULL , my application is crashed.

Correct Code:

NSArray *objects = [NSArray arrayWithObjects:@"bf45c093e542f057c123ae7d6",@"TruckIdStr",@"DriverIdStr",@"Member_id",@"amount",@"GrandTotalString",@"vendor_id",@"delivery_date",@"RcvdAddressId",@"create_Id",@"Transaction_Id",@"SelectedItemsArray", nil];

If you need to replace to Blank string if there is NULL in it.

Replace NULL string into blank:

  NSString* strValue = NULL;
if (strValue == nil || strValue == (id)[NSNull null]) {
// Set Blank to string
strValue = @"";
} else {
// You don't have NULL in your string, so you can use it directly
}

Combining 2 Array into dictionary

Just like that:

let arr1 = ["a", "b", "c"]
let arr2 = ["1", "2", "3"]

let dictionary = Dictionary(uniqueKeysWithValues: zip(arr1, arr2))

NSDictionary: method only defined for abstract class. My app crashed

Are you subclassing NSDictionary? That's not a common thing to do in Cocoa-land, which might explain why you're not seeing the results you expect.

NSDictionary is a class cluster. That means that you never actually work with an instance of NSDictionary, but rather with one of its private subclasses. See Apple's description of a class cluster here. From that doc:

You create and interact with instances of the cluster just as you would any other class. Behind the scenes, though, when you create an instance of the public class, the class returns an object of the appropriate subclass based on the creation method that you invoke. (You don’t, and can’t, choose the actual class of the instance.)

What your error message is telling you is that if you want to subclass NSDictionary, you have to implement your own backend storage for it (for example by writing a hash table in C). It's not just asking you to declare that method, it's asking you to write it from scratch, handling the storage yourself. That's because subclassing a class cluster directly like that is the same as saying you want to provide a new implementation for how dictionaries work. As I'm sure you can tell, that's a significant task.

Assuming you definitely want to subclass NSDictionary, your best bet is to write your subclass to contain a normal NSMutableDictionary as a property, and use that to handle your storage. This tutorial shows you one way to do that. That's not actually that hard, you just need to pass the required methods through to your dictionary property.

You could also try using associative references, which "simulate the addition of object instance variables to an existing class". That way you could associate an NSNumber with your existing dictionary to represent the tag, and no subclassing is needed.

Of course, you could also just have tag as a key in the dictionary, and store the value inside it like any other dictionary key.



Related Topics



Leave a reply



Submit