nsdictionary

NSDictionary to NSData and NSData to NSDictionary in Swift

半城伤御伤魂 提交于 2019-11-27 10:06:15
问题 I am not sure if I am using the dictionary or the data object or both incorrectly. I m trying to get used to the switch to swift but I'm having a little trouble. var dictionaryExample : [String:AnyObject] = ["user":"UserName", "pass":"password", "token":"0123456789", "image":0] // image should be either NSData or empty let dataExample : NSData = dictionaryExample as NSData I need the NSDictionary to encode to an NSData object as well as taking that NSData object and decode it into a

How can I convert NSDictionary to NSData and vice versa?

落花浮王杯 提交于 2019-11-27 09:59:42
I am sending NSString and UIImage using bluetooth. I decided to store both in a NSDictionary and then convert the dictionary to NSData . My question is how to convert NSDictionary to NSData and visa versa? Anh Nguyen NSDictionary -> NSData: NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:myDictionary]; NSData -> NSDictionary: NSDictionary *myDictionary = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:myData]; LeonS NSDictionary -> NSData: NSMutableData *data = [[NSMutableData alloc] init]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]

How can i get Original order of NSDictionary/NSMutableDictionary?

北慕城南 提交于 2019-11-27 09:27:59
i have created NSMutableDictionary with 10 keys.Now i want to access NSMutableDictionary keys in a same order as it was added to NSMutableDictionary (using SetValue:* forKey:* ); How can i achieve that ? If you absolutely must use a dictionary container, you have to use a key that is sortable by the order in which you add key-value pairs. Thus, when creating your dictionary, you use a key that is an auto-incrementing integer or similar. You can then sort on the (integer) keys and retrieve the values associated with those keys. If you do all of that, however, you may as well just use an

Replace occurrences of NSNull in nested NSDictionary

懵懂的女人 提交于 2019-11-27 09:16:37
This question is similar to this question , however this method only works on the root level of the dictionary. I'm looking to replace any occurrence of NSNull values with an empty string, so that I can save the full dictionary to a plist file (if i add it with the NSNull's the file won't write). My dictionary, however, has nested dictionaries inside it. Like this: "dictKeyName" = { innerStrKeyName = "This is a string in a dictionary"; innerNullKeyName = "<null>"; innerDictKeyName = { "innerDictStrKeyName" = "This is a string in a Dictionary in another Dictionary"; "innerDictNullKeyName" = "

Plist Array to NSDictionary

懵懂的女人 提交于 2019-11-27 08:42:59
I have a plist: <plist version="1.0"> <array> <dict> <key>name</key> <string>Alabama</string> <key>abreviation</key> <string>AL</string> <key>date</key> <string>1819</string> <key>population</key> <string>4,627,851</string> <key>capital</key> <string>Montgomery</string> <key>largestCity</key> <string>Birmingham</string> </dict> <dict> <key>name</key> <string>Alaska</string> <key>abreviation</key> <string>AK</string> <key>date</key> <string>1959</string> <key>population</key> <string>683,478</string> <key>capital</key> <string>Juneau</string> <key>largestCity</key> <string>Anchorage</string> <

Swift (beta 3) “NSDictionary? does not conform to protocol 'Equatable'”

青春壹個敷衍的年華 提交于 2019-11-27 07:19:08
问题 I've had a fair few warnings and errors in my Swift code since updating to the latest Xcode 6 DP3. Most have been resolved by adopting the newly changed syntax however there is one error which seems strange. The following code gives the error Type 'NSDictionary?' does not conform to protocol 'Equatable' : if (launchOptions != nil && launchOptions![UIApplicationLaunchOptionsRemoteNotificationKey] != nil) { Does anyone have a solution? I'm probably overlooking something simple here..! Thanks

How to add userInfo to a UIAlertView?

时光毁灭记忆、已成空白 提交于 2019-11-27 07:08:10
I would like to know how to add a userInfo object, or any NSDictionary, to a UIAlertView? Thank you. If you are on > iOS 4.0 (for blocks) and you only want one or two buttons, you could use this category I made: https://github.com/rsaunders100/UIAlertView-Blocks It bypasses the need to add user info since you put your click handeling function straight into the alert. e.g. #import UIAlertView+Blocks.h ... ... NSString* myUserInfo = @"example"; [UIAlertView displayAlertWithTitle:@"Example Alert View With Blocks" message:nil leftButtonTitle:@"Ok" leftButtonAction:^{ NSLog(@"%@", myUserInfo); }

Converting NSDictionary to XML

◇◆丶佛笑我妖孽 提交于 2019-11-27 07:06:59
问题 I need to Post data in XML format. The server accepts a specific xml format. I don't want to write the xml by hand, what i want to do is create a NSMutableDictionary populate it and from NSMutableDictionary convert it XML. I use this: [NSPropertyListSerialization dataWithPropertyList:data format:NSPropertyListXMLFormat_v1_0 options:0 The sample return is this: <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"> <dict>

How to append elements into a dictionary in Swift?

此生再无相见时 提交于 2019-11-27 06:42:30
I have a simple Dictionary which is defined like: var dict : NSDictionary = [ 1 : "abc", 2 : "cde"] Now I want to add an element into this dictionary: 3 : "efg" How can I append 3 : "efg" into this existing dictionary? Antonio You're using NSDictionary . Unless you explicitly need it to be that type for some reason, I recommend using a Swift dictionary. You can pass a Swift dictionary to any function expecting NSDictionary without any extra work, because Dictionary<> and NSDictionary seamlessly bridge to each other. The advantage of the native Swift way is that the dictionary uses generic

for each loop in Objective-C for accessing NSMutable dictionary

五迷三道 提交于 2019-11-27 05:50:53
I am finding some difficulty in accessing mutable dictionary keys and values in Objective-C. Suppose I have this: NSMutableDictionary *xyz=[[NSMutableDictionary alloc] init]; I can set keys and values. Now, I just want to access each key and value, but I don't know the number of keys set. In PHP it is very easy, something as follows: foreach ($xyz as $key => $value) How is it possible in Objective-C? zneak for (NSString* key in xyz) { id value = xyz[key]; // do stuff } This works for every class that conforms to the NSFastEnumeration protocol (available on 10.5+ and iOS), though NSDictionary