Too many arguments on string creation - ios

I Downloaded a JSON file and then I try to create a string with one of the values of the downloaded dictionary.
The code I'm using:
NSMutableDictionary *tempDict = [listaShowsFiltrada objectAtIndex:indexPath.row];
NSLog(#"%#", tempDict);
cell.labelLugar.text = [tempDict objectForKey:#"local"]; //This is working
NSString *stringCidadeEstado = [[NSString alloc] initWithString:#"%# - %#", [[tempDict objectForKey:#"cidade"] stringValue], [[tempDict objectForKey:#"estado"] stringValue]]; //This is not working
The NSLog:
2014-04-27 11:57:46.645 {
cidade = "S\U00e3o Paulo";
"cod_agenda" = 15;
data = "05/05/2014";
estado = SP;
"hora_show" = "22:00";
local = "Teatro Renaissance";
}
2014-04-27 11:57:46.655 {
cidade = "Belo Horizonte";
"cod_agenda" = 16;
data = "06/06/2014";
estado = MG;
"hora_show" = "22:00";
local = "Teatro Sesi Minas";
}
2014-04-27 11:57:46.656 {
cidade = Curitiba;
"cod_agenda" = 14;
data = "14/06/2014";
estado = PR;
"hora_show" = "22:00";
local = "Teatro Regi Vogue";
}
The JSON file:
[{"cod_agenda":"15","local":"Teatro Renaissance","cidade":"S\u00e3o Paulo","estado":"SP","data":"05\/05\/2014","hora_show":"22:00"},{"cod_agenda":"16","local":"Teatro Sesi Minas","cidade":"Belo Horizonte","estado":"MG","data":"06\/06\/2014","hora_show":"22:00"},{"cod_agenda":"14","local":"Teatro Regi Vogue","cidade":"Curitiba","estado":"PR","data":"14\/06\/2014","hora_show":"22:00"}]
The error:
Too many arguments to method call, expected 1, have 3.

The method initWithString takes only one argument, you want to use initWithFormat:
NSString *stringCidadeEstado = [[NSString alloc] initWithFormat:#"%# - %#", [[tempDict objectForKey:#"cidade"] stringValue], [[tempDict objectForKey:#"estado"] stringValue]];

Related

How can I add in multiple items in model by using array method?

I have an original code as below which is working properly:-
- (ZYSideSlipFilterRegionModel *)commonFilterRegionModelWithKeyword:(NSString *)keyword selectionType:(CommonTableViewCellSelectionType)selectionType {
ZYSideSlipFilterRegionModel *model = [[ZYSideSlipFilterRegionModel alloc] init];
model.containerCellClass = #"SideSlipCommonTableViewCell";
model.regionTitle = keyword;
model.customDict = #{REGION_SELECTION_TYPE:#(selectionType)};
model.itemList = #[[self createItemModelWithTitle:[NSString stringWithFormat:#"Local"] itemId:#"0" selected:NO],
[self createItemModelWithTitle:[NSString stringWithFormat:#"Oversea"] itemId:#"1" selected:NO]];
return model;
}
Now, I plan to change the static value (Oversea / local) to dynamic value. But only 1 item will be displayed.
for (int i = 0; i < filteredArray.count; i++) {
int intItemID = i + 1;
NSString *myNewString = [NSString stringWithFormat:#"%i", intItemID];
model.itemList = #[[self createItemModelWithTitle:[filteredArray[i] valueForKey:#"attribute_name"] itemId:myNewString selected:NO] ];
}
How can I put 2 item in model.itemList? Please help. Thank you.
You can use this way
//step:1 fetch Dictionary like this
for (int i = 0; i < filteredArray.count; i++)
{
NSMutableDictionary *dict = (NSMutableDictionary *)filteredArray[i] ;
int intItemID = i + 1;
NSString *myNewString = [NSString stringWithFormat:#"%i", intItemID];
model.itemList = #[[self createItemModelWithTitle:dict];
}
//Step 2 : You can define your method for Model Like this
- (CommonItemModel *)createItemModelWithTitle:(NSMutableDictionary *)dictModel
{
CommonItemModel *model = [[CommonItemModel alloc] init];
model.itemId = [dictModel valueForKey : #"itemId"];
model.itemName = [dictModel valueForKey:#"itemTitle"];
model.selected = [dictModel valueForKey:[NSNumber numberWithBool:
[[dictModel valueForKey:#"selected"]]]];
return model;
}
//One more thing you are write this in first step
model.itemList = #[[self createItemModelWithTitle:dict];
But the method only return the Model class (CommonItemModel) so if you need any help you shared here
Thanks :)

How to retrieve specific value of key in json?

this is my json content.
[
{
"sha":"30eae8a47d0203ac81699d8fc2ab2632de2d0bba",
"commit":{
"author":{
"name":"Madhura Bhave",
"email":"mbhave#pivotal.io",
"date":"2017-03-23T23:14:32Z"
},
"committer":{
"name":"Madhura Bhave",
"email":"mbhave#pivotal.io",
"date":"2017-03-23T23:14:32Z"
},
"message":"Merge branch '1.5.x'",
}
}
]
and this is my main.i just want to retrieve key value from message and name,email,date from committer dictionary.i got stuck how to do that.
NSMutableArray *CommitArray = [[NSMutableArray alloc] init];
for (NSDictionary *CommitDictionary in CommitJson) {
CommitDict *commitDictObj = [[CommitDict alloc] init];
commitDictObj.message = [CommitDictionary objectForKey:#"message"];
for (NSDictionary *CommitterDictionary in [CommitDictionary objectForKey:#"committer"]) {
Committer *author = [[Committer alloc] init];
author.name = [CommitterDictionary objectForKey:#"name"];
author.email = [CommitterDictionary objectForKey:#"email"];
author.date = [CommitterDictionary objectForKey:#"date"];
}
[CommitArray addObject:commitDictObj];
}
for (int i =0 ; i < [CommitArray count] ; i++){
CommitDict *commitDictObj = [CommitArray objectAtIndex:i];
NSLog(#"Commit Message: %#", commitDictObj.message);
}
return 0;
}
}
i try fetch the json and display it value of message,name,email and date.how can i log the value of message, name, email and date?
Your array contains a dictionary, and that dictionary contains the commit dictionary, not the commit dictionary directly. Replace that part of your code:
for (NSDictionary *CommitDictionary in CommitJson) {
CommitDict *commitDictObj = [[CommitDict alloc] init];
With that:
for (NSDictionary *shaCommitDictionary in CommitJson) {
CommitDict *commitDictObj = [[CommitDict alloc] init];
NSDictionary *CommitDictionary = [shaCommitDictionary objectForKey:#"commit"];
(1) Convert JSON to NSDictionary
NSData *jsonData= ... // Assume you got the data already loaded
NSError *error = nil;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
(2) Access the dictionary values (fast enumeration available by now!!
NSString *message = dictionary[#"message"];
NSDictionary *author = dictionary[#"author"];
NSString *name = author[#"author"];
NSString *email = author[#"author"];
NSString *date = author[#"author"];
// OR:
// NSString *name = dictionary[#"author"][#"author"];
// NSString *email = dictionary[#"author"][#"author"];
// NSString *date = dictionary[#"author"][#"author"];
And thats it. I think the tricky thing is to get the JSON Data to the NSDictionary?
See here: https://stackoverflow.com/a/30561781/464016

iOS PayUMoney SDK-key is not valid

I have integrate payumoney payment gateway. And I am able to create correct hash with the key, but at the end am getting an alert message as "key is not valid".what would be the problem?
Here is the code. I use the link "https://www.payumoney.com/payment-gateway-integration-guide.html" to download the payumoney SDK.
- (IBAction)startPaymentTapped:(id)sender {
[self setPaymentParameters];
//Start the payment flow
PUMMainVController *paymentVC = [[PUMMainVController alloc] init];
UINavigationController *paymentNavController = [[UINavigationController alloc] initWithRootViewController:paymentVC];
[self presentViewController:paymentNavController
animated:YES
completion:nil];
}
- (void)setPaymentParameters {
self.params = [PUMRequestParams sharedParams];
self.params.environment = PUMEnvironmentProduction;
self.params.amount = self.amount.text;
self.params.key = #"mykeyvalue";
self.params.merchantid = #"merchantid";
self.params.txnid = [self getRandomString:2];
self.params.surl = #"https://www.payumoney.com/mobileapp/payumoney/success.php";
self.params.furl = #"https://www.payumoney.com/mobileapp/payumoney/failure.php";
self.params.delegate = self;
self.params.firstname = self.firstname.text;
self.params.productinfo = self.productinfo.text;
self.params.email = self.email.text;
self.params.phone = #"";
self.params.udf1 = #"";
self.params.udf2 = #"";
self.params.udf3 = #"";
self.params.udf4 = #"";
self.params.udf5 = #"";
self.params.udf6 = #"";
self.params.udf7 = #"";
self.params.udf8 = #"";
self.params.udf9 = #"";
self.params.udf10 = #"";
self.params.hashValue = [self getHash];
}
- (NSString *)getRandomString:(NSInteger)length {
NSMutableString *returnString = [NSMutableString stringWithCapacity:length];
NSString *numbers = #"0123456789";
// First number cannot be 0
[returnString appendFormat:#"%C", [numbers characterAtIndex:(arc4random() % ([numbers length]-1))+1]];
for (int i = 1; i < length; i++) {
[returnString appendFormat:#"%C", [numbers characterAtIndex:arc4random() % [numbers length]]];
}
return returnString;
}
- (NSString*)getHash {
NSString *hashSequence = [NSString stringWithFormat:#"mykeyvalue|%#|%#|%#|%#|%#|||||||||||salt",self.params.txnid, self.params.amount, self.params.productinfo,self.params.firstname, self.params.email];
NSString *rawHash = [[self createSHA512:hashSequence] description];
NSString *hash = [[[rawHash stringByReplacingOccurrencesOfString:#"<" withString:#""]stringByReplacingOccurrencesOfString:#">" withString:#""]stringByReplacingOccurrencesOfString:#" " withString:#""];
return hash;
}
- (NSData *) createSHA512:(NSString *)source {
const char *s = [source cStringUsingEncoding:NSASCIIStringEncoding];
NSData *keyData = [NSData dataWithBytes:s length:strlen(s)];
uint8_t digest[CC_SHA512_DIGEST_LENGTH] = {0};
CC_SHA512(keyData.bytes, (CC_LONG)keyData.length, digest);
NSData *output = [NSData dataWithBytes:digest length:CC_SHA512_DIGEST_LENGTH];
NSLog(#"out --------- %#",output);
return output;
}
Please check data when you send payumoney for create hash key with server api data for create your hash key.
self.params.environment = PUMEnvironmentProduction;
Through the above code you can pay using net banking successfully. If you want to test
your payment gateway for dummy credit/debit card some changes are required. I am
listing it down. Try it.
Instead of
self.params.environment = PUMEnvironmentProduction; //write
self.params.environment = PUMEnvironmentTest;
change the urls as
self.params.surl = #"https://test.payumoney.com/mobileapp/payumoney/success.php";
self.params.furl = #"https://test.payumoney.com/mobileapp/payumoney/failure.php";
// We need test url for testing purpose.
Make sure you have activated your payumoney account by clicking on link that you got in mail.
Use the below dummy master card details
Test Card Name: any name
Test Card Number: 5123456789012346
Test CVV: 123
Test Expiry: May 2017

How to merge 2 NSDictionaries in NSMutableArray having same value in iOS?

I have an NSMutableArray with NSDictionaries like below format
(
{
"login_id" = 95;
consDays = "Mon,Tue";
consEndTime = 12600000;
consStartTime = 9000000;
duration = 10;
id = 58;
isActive = 1;
},
{
"login_id" = 95;
consDays = "Wed,Thur";
consEndTime = 41400000;
consStartTime = 23400000;
duration = 10;
id = 59;
isActive = 1;
},
{
"login_id" = 98;
consDays = "Mon,Tue,Wed,Thur,Fri,Sat,Sun";
consEndTime = 45000000;
consStartTime = 9000000;
duration = 30;
id = 60;
isActive = 1;
},
I can see here first 2 object's with "login_id" 95 are same and i need to merge the contents and make something like this one
{
"login_id" = 95;
consDays = "Mon,Tue-Wed,Thur";
consEndTime = 12600000-41400000;
consStartTime = 9000000-23400000;
duration = 10;
id = 58;
isActive = 1;
},
i have tried with for loop and its not getting clear
for(int i=0;i<[[arr valueForKey:#"data"] count];i++){
NSString *bloginId=[NSString stringWithFormat:#"%#",[[[arr valueForKey:#"data"] valueForKey:#"branchLogin_id"] objectAtIndex:i]];
for(int j=0;j<[[arr valueForKey:#"data"] count];j++){
NSString *firstId=[NSString stringWithFormat:#"%#",[[[arr valueForKey:#"data"] valueForKey:#"branchLogin_id"] objectAtIndex:i]];
NSString *secondId=[NSString stringWithFormat:#"%#",[[[arr valueForKey:#"data"] valueForKey:#"branchLogin_id"] objectAtIndex:j]];
NSLog(#"%#-%#",firstId,secondId);
if([firstId isEqualToString:secondId]){
NSLog(#"%#",[consultationDetailsArray valueForKey:#"branchLogin_id"]);
if([[consultationDetailsArray valueForKey:#"branchLogin_id"] containsObject:secondId]){
NSString *str=[NSString stringWithFormat:#"%#,%#",[[consultationDetailsArray valueForKey:#"consDays"] objectAtIndex:j],[[[arr valueForKey:#"data"] valueForKey:#"consDays"] objectAtIndex:j]];
NSLog(#"consDays %#",str);
}else{
[consultationDetailsArray addObject:[[arr valueForKey:#"data"] objectAtIndex:j]];
}
}
}
}
can anyone help me ?
Ironically I am doing just this same thing right now. Looks like the easiest way is to use a NSMutableDictionary.
// Assume two starting dictionaries.
dictionaryOne;
dictionaryTwo;
//
// Create a mutable copy of the first
NSMutableDictionary *merged = dictionaryOne.mutableCopy;
[merged addEntriesFromDictionary: dictionaryTwo];
//
// merged now contains all keys of both dictionaries
// keys that are in both dictionaries will have values
// from dictionaryTwo.
Hope this helps.

Order NSArray with objects

I have an NSDictionary with the following data:
(lldb) po allFriends
{
71685207018702188 = {
id = 71685207018702188;
name = "mikeziri ";
username = mi;
};
93374822540641772 = {
id = 93374822540641772;
name = "Alan Weclipse";
username = zuka;
};
96553685978449395 = {
id = 96553685978449395;
name = "Monica Weclipse";
username = amonica;
};
96556113096345076 = {
id = 96556113096345076;
name = Xavier;
username = branko;
};
97017008427632119 = {
id = 97017008427632119;
name = "Dario Weclipse";
username = tarzan;
};
}
I'm sorting these objects based on the name, if they don't have a name, i will use the username. To do that, i create a new NSDictionary with the name and id and at the end of the method i sort them by name. The code to sort them is the following:
- (NSArray*)orderFriends
{
NSMutableDictionary* newFriendsDict = [[NSMutableDictionary alloc] init];
for (int i=0; i<[allFriends count];i++)
{
NSMutableDictionary* friendsDict = [[NSMutableDictionary alloc] init];
NSDictionary* friend = [allFriends objectForKey:[NSString stringWithFormat:#"%#", [sortedKeysFriends objectAtIndex:i]]];
if ([[friend objectForKey:#"name"] length] != 0)
{
[friendsDict setObject:[friend objectForKey:#"id"] forKey:#"id"];
[friendsDict setObject:[NSString stringWithFormat:#"%#", [friend objectForKey:#"name"]] forKey:#"name"];
}
else
{
[friendsDict setObject:[friend objectForKey:#"id"] forKey:#"id"];
[friendsDict setObject:[NSString stringWithFormat:#"%#", [friend objectForKey:#"username"]] forKey:#"name"];
}
[newFriendsDict setObject:friendsDict forKey:[NSNumber numberWithInt:i]];
}
NSArray* sp = nil;
sp = [[newFriendsDict allValues] sortedArrayUsingComparator:^(id obj1, id obj2){
NSString *one = [NSString stringWithFormat:#"%#", [obj1 objectForKey:#"name"]];
NSString *two = [NSString stringWithFormat:#"%#", [obj2 objectForKey:#"name"]];
return [one compare:two];
}];
return sp;
}
The problem is that the end result is wrong:
(lldb) po sp
<__NSArrayI 0x160491a0>(
{
id = 93374822540641772;
name = "Alan Weclipse";
},
{
id = 97017008427632119;
name = "Dario Weclipse";
},
{
id = 96553685978449395;
name = "Monica Weclipse";
},
{
id = 96556113096345076;
name = Xavier;
},
{
id = 71685207018702188;
name = "mikeziri ";
},
)
Case sensitive. make all string small or big.
You could also just change
return [one compare:two];
to
return [one compare:two options: options:NSCaseInsensitiveSearch];
Than it will be ordered alphabetically, no matter if upper or lower case...
Several things: There is no reason to build different dictionaries in order to sort, and good reason NOT to do so.
You already found the method sortedArrayUsingComparator. That takes a block that is used to compare pairs of objects, and returns a sorted array. You can use that method to implement any sorting criteria you want.
I would suggest writing a comparator block that compares the name properties of your objects unless it's blank, and uses username if that's blank. It would only be a few lines of code:
NSArray *sortedFriends = [[allFriends allValues] sortedArrayUsingComparator:
^(NSDictionary *obj1, NSDictionary *obj2)
{
NSString* key1 = obj1[#"name"] ? obj1[#"name"] : obj1[#"username"];
NSString* key2 = obj2[#"name"] ? obj2[#"name"] : obj2[#"username"];
return [key1 caseInsensitiveCompare: key2];
}];
EDIT: I just noticed (from your edit of my post) that you are starting from a dictionary, not an array. So what you want to do is to create a sorted array of all the values in the dictionary? Is it acceptable to discard the keys for all the items in your dictionary, and end up with a sorted array of the values?
The other thing you could do would be to build an array of the dictionary keys, sorted based on your sort criteria. Then you could use the array of keys to fetch the items from your dictionary in sorted order.

Resources