This was the question I had asked in order to write data into firebase through an android app, I want to represent the information in the same way in an iphone app.
I am using a dictionary to represent the key-value pairs.
f = [[Firebase alloc] initWithUrl:#"https://ums-ios.firebaseio.com/"];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
#"latitude",[[NSNumber numberWithFloat:latitude] stringValue],
#"longitude",[[NSNumber numberWithFloat:longitude] stringValue],
#"timestamp",[[NSNumber numberWithDouble:timeStamp] stringValue],
nil];
Firebase* tempRef = [f childByAppendingPath:#"mobileNum"];
[tempRef setValue:dictionary];
I am getting an exception when I try to run this. But when I replace the NSDictionary with NSArray my data is getting mapped to array indices, which is not what I would require.
any suggestions ?
I think it's unrelated, butyour dictionary is backwards. I also find it terribly annoying, and I feel like I always have to double check, but values come before keys for iOS
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[[NSNumber numberWithFloat:latitude] stringValue], #"latitude",
[[NSNumber numberWithFloat:longitude] stringValue], #"longitude",
[[NSNumber numberWithDouble:timeStamp] stringValue], #"timestamp",
nil];
The full code would be this
float latitude = 30.472;
float longitude = 42.467;
double timeStamp = NSTimeIntervalSince1970;
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[[NSNumber numberWithFloat:latitude] stringValue], #"latitude",
[[NSNumber numberWithFloat:longitude] stringValue], #"longitude",
[[NSNumber numberWithDouble:timeStamp] stringValue], #"timestamp",
nil];
Firebase * f = [[Firebase alloc]initWithUrl:#"https://ums-ios.firebaseio.com/"];
Firebase* tempRef = [f childByAppendingPath:#"mobileNum"];
[tempRef setValue:dictionary];
This will create the following URL scheme (added .json so you can see easy)
https://ums-ios.firebaseio.com/mobileNum/.json = the dictionary
https://ums-ios.firebaseio.com/mobileNum/latitude/.json = value for latitude
https://ums-ios.firebaseio.com/mobileNum/longitude/.json = value for longitude
https://ums-ios.firebaseio.com/mobileNum/timestamp/.json = value for timestamp
A note on security,
I just saved this data to your firebase, make sure you update security before releasing!
Related
Want to merge two NSDictionary or NSMutableDictionary only with dict1 values. like [NSMutableDictionary addEntriesFromDictionary:] and intersectSet (from NSMutableSet).
Dict 1 = #{#”One”: #””, #”Two”: #”"}
Dict 2 = #{#”One”: #”1”, #”Two”: 2, #”Three”: #”3"}
My expect output after merge = #{#”One”: #”1”, #”Two”: #”2"}
UPDATE
I am already tried with [Dict1 addEntriesFromDictionary:Dict2] this one give answer #{#”One”: #”1”, #”Two”: 2, #”Three”: #”3"}
Also I am tried with NSMutableSet
NSMutableSet *keysInA = [NSMutableSet setWithArray:tempDict.allKeys];
NSSet *keysInB = [NSSet setWithArray:tempDict1.allKeys];
[keysInA intersectSet:keysInB];
NSLog(#"keys in A that are not in B: %#", keysInA);
This one give output One, Two, Three.
So [NSMutableSet setWithArray:] and [NSMutableDictionary addEntriesFromDictionary:] won't give what I expected.
If above code have anything work let clear me. Thanks.
NSDictionary *dict1 = #{#"One": #"", #"Two": #"6"};
NSDictionary *dict2 = #{#"One": #"1", #"Two": #"2", #"Three": #"3"};
NSDictionary * dict = [dict2 dictionaryWithValuesForKeys:[dict1 allKeys]];
NSLog(#"dict %#",dict);
// Log value
dict {
One = 1;
Two = 2;
}
Here what I expected.
try this...
//Assuming both dictionaries have same kind of keys
NSMutableDictionary *dict1 = #{#"One" : #"", #"Two": #"", #"Three" : #""}.mutableCopy;
NSDictionary *dict2 = #{#"One" : #"1", #"Two": #"2", #"Three" : #"3",#"Four":#"4"};
for (NSString *key in dict1.allKeys)
{
if (dict2[key])
{
dict1[key] = dict2[key];
}
}
NSLog(#"Updated dict %#",dict1);
I just started developing iOS using objective-c, and I've had some trouble getting used to the syntax. i made a signup form in objective c how i can stored fields information in NSDictionary
// You have already all objects
NSString *loginString = #"user1";
NSString *passwordString = #"password1";
NSDictionary *dict = #{#"login":loginString, #"password":passwordString};
// You do not have all objects
NSMutableDictionary *mDict = [NSMutableDictionary new];
//When you have loginString you add it to the mDict
[mDict setObject:#"user2" forKey:#"login"];
//When you have passwordString you add it to the mDict
[mDict setObject:#"password2" forKey:#"password"];
try this
NSDictionary *dict=#{#"firstname" : txtAddFirstName.text,
#"lastname" :txtAddLastName.text,
#"region" :txtRegion.text,
#"street" :txtVAddress.text,
#"company" :txtAddCompny.text,
#"city" :txtCity.text,
#"country" : countryCode,
#"postcode" :txtZip.text,
#"telephone" :txtTelephone.text,
#"fax" :txtFax.text,
};
I' want to filter NSARRAY based on an object named id, I've specific set of Ids that I want to filter and want to be first in NSARRAY.
I stored the following Ids as NSNUMEBR
NSNumber *A = [NSNumber numberWithInt:1122];
NSNumber *B = [NSNumber numberWithInt:1345];
NSNumber *C = [NSNumber numberWithInt:1667];
NSNumber *D = [NSNumber numberWithInt:1223];
NSNumber *E = [NSNumber numberWithInt:1213];
NSNumber *F = [NSNumber numberWithInt:1123];
NSNumber *G = [NSNumber numberWithInt:1555];
NSNumber *H = [NSNumber numberWithInt:1666];
NSNumber *I = [NSNumber numberWithInt:1567];
These are the set of ids that I want to filter and want to be first in my NSARRAY (Can be NSMutableArray for operation)
EDIT 1:
the NSARRAY is basically getting the id object as
Ids = [dict valueForKey:#"id"];
That selective ids are stored in NSNUMBER A to I
It is unclear what you are asking, as indicated by the down votes and comments. But let's see if we can help. I think the following pseudo-code algorithm is what you are asking for:
MutableArray frontItems, rearItems;
for every item in sourceArray
if item["id"] is in the collection of specific IDs
then add item to end of frontItems
else add item to end of rearItems
add rearItems to end of frontItems to give result
Write that in Objective-C and I think you have what you want.
HTH
//Creat array have all item : A->I
NSNumber *A = [NSNumber numberWithInt:1122];
NSNumber *B = [NSNumber numberWithInt:1345];
NSNumber *C = [NSNumber numberWithInt:1667];
NSNumber *D = [NSNumber numberWithInt:1223];
NSNumber *E = [NSNumber numberWithInt:1213];
NSNumber *F = [NSNumber numberWithInt:1123];
NSNumber *G = [NSNumber numberWithInt:1555];
NSNumber *H = [NSNumber numberWithInt:1666];
NSNumber *I = [NSNumber numberWithInt:1567];
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:A,B,C,...,I, nil];
for (NSNumber *idx in arr) {
// To do
}
I'm trying to iterate over an NSMutableDictionary and I cannot seem to get what I want. I have a dictionary mapping strings to colors like so...
squareColors = [NSMutableDictionary dictionaryWithObjects: [NSMutableArray arrayWithObjects:
[NSNumber numberWithInt:0],
[NSNumber numberWithInt:0],
[NSNumber numberWithInt:0],
[NSNumber numberWithInt:0],
[NSNumber numberWithInt:0],
nil]
forKeys: [NSMutableArray arrayWithObjects:
#"yellow",
#"blue",
#"green",
#"purple",
#"orange",
nil]];
Over time the value of each entry will increase. Every once in a while I want to look into the dictionary and select the color with the highest count. How might I do that? Here's what I'm trying, but I'm unfamiliar with blocks.
__block int mostSquares = 0;
__block NSString* color = #"";
/* Look through the dictionary to find the color with the most number of squares */
[squareColors enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(#"%# => %#", key, obj);
NSInteger count = [key integerValue];
if (count > mostSquares)
{
color = key;
mostSquares = count;
}
}];
You have a very simple bug in your code. This line:
NSInteger count = [key integerValue];
should be:
NSInteger count = [obj integerValue];
'key' is the color name, obj is the number. As you have it, count gets set to 0 for each iteration because calling integerValue on a non-numeric string gives you 0.
Simple solution using your example:
NSMutableArray *arrayNumbers = [NSMutableArray arrayWithObjects:
[NSNumber numberWithInt:0],
[NSNumber numberWithInt:1],
[NSNumber numberWithInt:2],
[NSNumber numberWithInt:6],
[NSNumber numberWithInt:4],
nil];
NSMutableArray *arrayColours = [NSMutableArray arrayWithObjects:
#"yellow",
#"blue",
#"green",
#"purple",
#"orange",
nil];
NSMutableDictionary *squareColors = [NSMutableDictionary dictionaryWithObjects:arrayNumbers
forKeys:arrayColours];
NSUInteger indexOfArray = [arrayNumbers indexOfObject:[arrayNumbers valueForKeyPath:#"#max.intValue"]];
NSLog (#"Colour with largest value is %#", [arrayColours objectAtIndex:indexOfArray]);
Can you store your Keys into an array and iterate using that array? That would probably be the most efficient solution, since you'll need to know every key anyway.
I am using NSUserDefaults to store some integer settings for my app, but I want to register default values for these settings, using code like this:
NSDictionary *appDefaults = [[NSDictionary alloc] initWithObjectsAndKeys:(NSInteger)0, #"PMChordColour", (NSInteger)1, #"PMTextColour", (NSInteger)0, #"PMBackgroundColour", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
The values are read in my app using
NSInteger setColour = [[NSUserDefaults standardUserDefaults] integerForKey:#"PMChordColour"];
The problem with this is that NSDictionary does not allow scalar values to be stored.
Is there any way of registering defaults that allows integers to be stored? I realise that I could use NSNumber but it seems like unnecessary overhead.
No, you have to use NSNumber. It's necessary overhead.
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:13], #"id", nil];
int integer = [[dict objectForKey:#"id"] intValue];