I want the value for key open_now in array - ios

Opening :- (
"Not Available",
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
"Not Available",
{
"open_now" = 1;
"weekday_text" = (
);
},
"Not Available"
)
This is an output of my NSArray. It contains some strings as Not Available and it contains some NSDictionary starting with { "open_now" = 1; "weekday_text" = ( ); } .I wanted the value of only open_now. I don't know how to get value of open_now in my new NSArray. Please anyone help me.

Yes, use the NSArray -valueForKey: method.
NSArray *extracted = [sourceArray valueForKey:#"a key"];

You can use valueForKeyPath method of NSArray to get value of particular key.
NSArray *aryValue = [YOUR_ARRAY valueForKeyPath:#"open_now"];

Related

SuiteScript 2.1 - Invalid Signature when creating TBA Access Token

Constantly getting Invalid Signature when trying to create a TBA access token.
The function to generate the OAuth1.0a signature is as follows:
const restletSignature = (options) => {
const HMAC_SHA256 = (key, text, output) => {
if ( !output) output = cryptoJS.enc.Base64;
return cryptoJS.HmacSHA256(text, key).toString(output);
};
const companyInfo = config.load({
type: config.Type.COMPANY_INFORMATION,
});
const makeNonce = () => {
var result = "", characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var charactersLength = characters.length;
for (var i = 0; i < this.nonce_length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
};
try {
log.debug('options', JSON.stringify(options));
if ( !options) {
options = {};
}
if ( !options.consumer) {
throw error.create({
name: 'MISSING_PARAMTER',
message: 'consumer option is required',
notifyOff: true,
});
}
if ( !options.token) {
throw error.create({
name: 'MISSING_PARAMTER',
message: 'token option is required',
notifyOff: true,
});
}
if ( !options.restletUrl) {
throw error.create({
name: 'MISSING_PARAMTER',
message: 'restletUrl option is required',
notifyOff: true,
});
}
if (typeof options.last_ampersand === 'undefined') {
this.last_ampersand = true;
}
else {
this.last_ampersand = options.last_ampersand;
}
this.httpMethod = options.httpMethod || "POST";
this.consumer = options.consumer;
this.token = options.token;
this.nonce_length = options.nonce_length || 32;
this.signature_method = options.signature_method || "HMAC-SHA256";
this.version = options.version || '1.0';
this.parameter_separator = options.parameter_separator || ', ';
this.realm = companyInfo.getValue({fieldId: "companyid"});
this.urls = {
system: "https://" + url.resolveDomain({hostType: url.HostType.APPLICATION}),
restlet: "https://" + url.resolveDomain({hostType: url.HostType.RESTLET}),
suitetalk: "https://" + url.resolveDomain({hostType: url.HostType.SUITETALK}),
forms: "https://" + url.resolveDomain({hostType: url.HostType.FORM}),
};
this.restletFullUrl = (options.restletUrl.indexOf("https://") == -1 ? urls.system : "") + options.restletUrl;
this.restletUrl = this.restletFullUrl.indexOf("?") > -1 ? this.restletFullUrl.split("?")[0] : "";
this.restletScript = this.restletFullUrl.match(/(?<=script\=)(.*)(?=\&deploy)/);
this.restletScript = this.restletScript.length ? this.restletScript[0] : null;
this.restletDeploy = this.restletFullUrl.match(/(?<=deploy\=)(.*)(?=\&)/);
this.restletDeploy = this.restletDeploy.length ? this.restletDeploy[0] : null;
this.encodedRestletUrl = encodeURIComponent(this.restletUrl);
this.parameters = {
deploy: this.restletDeploy,
oauth_consumer_key: this.consumer.key,
oauth_nonce: makeNonce(),
oauth_signature_method: this.signature_method,
oauth_timestamp: Math.round(+new Date() / 1000),
oauth_token: this.token.key,
oauth_version: "1.0",
script: this.restletScript,
};
this.encodedParameterString = '&deploy=' + encodeURIComponent(this.parameters.deploy);
this.encodedParameterString += '&oauth_consumer_key=' + encodeURIComponent(this.parameters.oauth_consumer_key);
this.encodedParameterString += '&oauth_nonce=' + encodeURIComponent(this.parameters.oauth_nonce);
this.encodedParameterString += '&oauth_signature_method=' + encodeURIComponent(this.parameters.oauth_signature_method);
this.encodedParameterString += '&oauth_timestamp=' + encodeURIComponent(this.parameters.oauth_timestamp);
this.encodedParameterString += '&oauth_token=' + encodeURIComponent(this.parameters.oauth_token);
this.encodedParameterString += '&oauth_version=' + encodeURIComponent(this.parameters.oauth_version);
this.encodedParameterString += '&script=' + encodeURIComponent(this.parameters.script);
this.encodedParameterString = encodeURIComponent(this.encodedParameterString);
this.baseString = this.httpMethod + "&";
this.baseString += this.encodedRestletUrl + "&";
this.baseString += this.encodedParameterString;
this.signatureKey = encodeURIComponent(this.consumer.secret) + "&" + encodeURIComponent(this.consumer.secret);
this.signature = encodeURIComponent(HMAC_SHA256(this.signatureKey,this.baseString, cryptoJS.enc.Base64 ) );
this.Authorization = "";
this.Authorization += 'OAuth oauth_signature="'+this.signature + '"'+this.parameter_separator;
this.Authorization += 'oauth_version="'+this.parameters.oauth_version + '"'+this.parameter_separator;
this.Authorization += 'oauth_nonce="'+this.parameters.oauth_nonce + '"'+this.parameter_separator;
this.Authorization += 'oauth_signature_method="'+this.parameters.oauth_signature_method + '"'+this.parameter_separator;
this.Authorization += 'oauth_consumer_key="'+this.parameters.oauth_consumer_key + '"'+this.parameter_separator;
this.Authorization += 'oauth_token="'+this.parameters.oauth_token + '"'+this.parameter_separator;
this.Authorization += 'oauth_timestamp="'+this.parameters.oauth_timestamp + '"'+this.parameter_separator;
this.Authorization += 'realm="'+this.realm + '"';
} catch (e) {
this.error = {
name: e.name,
message: e.message,
stack: e.stack,
};
} finally {
return this;
}
};
The Integration record has only "Token Based Authentication" ticked.
Consumer Key/Secret and Token ID/Secret are current, and I've tried setting them multiple times.
It's all running on Server Time (PST) so I don't think Timestamp zone is an issue, but I did try adding +36000 ms (for AEST) but no change.
Logging generates this data:
{
"auth": {
"last_ampersand": true,
"httpMethod": "POST",
"consumer": {
"key": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"secret": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
},
"token": {
"key": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"secret": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
},
"nonce_length": 32,
"signature_method": "HMAC-SHA256",
"version": "1.0",
"parameter_separator": ", ",
"realm": "XXXXXX",
"urls": {
"system": "https://XXXXXX.app.netsuite.com",
"restlet": "https://XXXXXX.restlets.api.netsuite.com",
"suitetalk": "https://XXXXXX.suitetalk.api.netsuite.com",
"forms": "https://XXXXXX.extforms.netsuite.com"
},
"restletFullUrl": "https://XXXXXX.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=576&deploy=1&compid=XXXXXX",
"restletUrl": "https://XXXXXX.restlets.api.netsuite.com/app/site/hosting/restlet.nl",
"restletScript": "576",
"restletDeploy": "1",
"encodedRestletUrl": "https%3A%2F%9999999.restlets.api.netsuite.com%2Fapp%2Fsite%2Fhosting%2Frestlet.nl",
"parameters": {
"deploy": "1",
"oauth_consumer_key": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"oauth_nonce": "58RInizRvCTdBGuXtugMRPhciQ0OC1xo",
"oauth_signature_method": "HMAC-SHA256",
"oauth_timestamp": 1639901494,
"oauth_token": "b49f1560e07771f8aad11418feae49be6162cf6fbd5d98bff0ae6aac70d4a30f",
"oauth_version": "1.0",
"script": "576"
},
"encodedParameterString": "%26deploy%3D1%26oauth_consumer_key%3XXXXXXXXXXXXXXXXXXXXXXXXXXXXX%26oauth_nonce%3D58RInizRvCTdBGuXtugMRPhciQ0OC1xo%26oauth_signature_method%3DHMAC-SHA256%26oauth_timestamp%3D1639901494%26oauth_token%3Db49f1560e07771f8aad11418feae49be6162cf6fbd5d98bff0ae6aac70d4a30f%26oauth_version%3D1.0%26script%3D576",
"baseString": "POST&https%3A%2F%2F9999999.restlets.api.netsuite.com%2Fapp%2Fsite%2Fhosting%2Frestlet.nl&%26deploy%3D1%26oauth_consumer_key%3DXXXXXXXXXXXXXXXXXXXXXXXXXXXXX%26oauth_nonce%3D58RInizRvCTdBGuXtugMRPhciQ0OC1xo%26oauth_signature_method%3DHMAC-SHA256%26oauth_timestamp%3D1639901494%26oauth_token%3Db49f1560e07771f8aad11418feae49be6162cf6fbd5d98bff0ae6aac70d4a30f%26oauth_version%3D1.0%26script%3D576",
"signatureKey": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX&XXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"signature": "U5iNYAo01jMm3WLdAHUH6R8M7tzooePZ3S0Jc5KQexQ%3D",
"Authorization": "OAuth oauth_signature=\"U5iNYAo01jMm3WLdAHUH6R8M7tzooePZ3S0Jc5KQexQ%3D\", oauth_version=\"1.0\", oauth_nonce=\"58RInizRvCTdBGuXtugMRPhciQ0OC1xo\", oauth_signature_method=\"HMAC-SHA256\", oauth_consumer_key=\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXX\", oauth_token=\"b49f1560e07771f8aad11418feae49be6162cf6fbd5d98bff0ae6aac70d4a30f\", oauth_timestamp=\"1639901494\", realm=\"9999999\""
}
}

Filter NSMutableDiction with Key

I need to filter this NSMutableDictionary on key value based where item_type= bad
i tried but not working
NSPredicate *pred=[NSPredicate predicateWithFormat:#"(item_type == %#)", #"Good"];
NSArray *resultArray = [[dummyDictionary allValues] filteredArrayUsingPredicate:pred];
NSLog(#"resultArray:%#",resultArray);
{
“Cat1” = (
{
"image_item" = “a”;
"item_type" = “Good”;
}
);
Cat2 = (
{
"image_item" = “b”;
"item_type" = Good;
},
{
"image_item" = “c”;
"item_type" = Bad;
},
);
Cat3 = (
{
"image_item" = “d”;
"item_type" = Good;
},
{
"image_item" = “e”;
"item_type" = Bad;
},
);
}
And need such output NSMutableDictionary
{
“Cat1” = (
);
Cat2 = (
{
"image_item" = “c”;
"item_type" = Bad;
},
);
Cat3 = (
{
"image_item" = “e”;
"item_type" = Bad;
},
);
}
NOTE: dummyDictionary is value i get from .plist

NSDictionary Not Getting First Item JSON

Maybe somebody can help. I am trying to get the location name for the google maps location and latitude in the URL below. As you can see from the JSON that it is first opening the results then the address_components. I am trying to get the first long_name and short_name in the JSON but instead the code I have below is only giving me the last long_name and short_name in the JSON.
Maybe someone has some type of idea, I have tried everything at this stage and nothing seems to solve the issue.
Thanks,
CurtisB
-(void)getPlaceName
{
NSString *urlString = [NSString stringWithFormat:#"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false",53.348623,-6.243089];
NSURL *url = [NSURL URLWithString:urlString];
//Next we need a NSURLSession instance
NSURLSession *session = [NSURLSession sharedSession];
//All tasks (there are three, see documentation) are created from an NSURLSession instance
//We want a DataTask
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//This "block" (of code) will be executed when the call is complete
//Serialize the JSON to Foundation objects
NSDictionary *geocodeDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
//Parse through the JSON to get to where we want, address_components array
NSArray *resultsArray = geocodeDictionary[#"results"];
NSDictionary *resultsDictionary = resultsArray[1];
NSArray *addressComponents = resultsDictionary[#"address_components"];
//Declare variables to hold desired results
NSString *longName;
NSString *shortName;
//The address_components array contains many dictionaries,
//we loop through each dictionary and check the types array
for (NSDictionary *addressComponentDictionary in addressComponents) {
longName = addressComponentDictionary[#"long_name"];
shortName = addressComponentDictionary[#"short_name"];
}
//Test log to see we are correct
NSLog(#"Long name for GetAddress is: %#", longName);
NSLog(#"Short Name is: %#", shortName);
}];
[dataTask resume];
}
JSON LOG:
[8948:3198407] {
results = (
{
"address_components" = (
{
"long_name" = "National College of Ireland";
"short_name" = "National College of Ireland";
types = (
premise
);
},
{
"long_name" = "Mayor Street Lower";
"short_name" = "Mayor Street Lower";
types = (
route
);
},
{
"long_name" = "International Financial Services Centre";
"short_name" = "International Financial Services Centre";
types = (
"sublocality_level_1",
sublocality,
political
);
},
{
"long_name" = Dublin;
"short_name" = Dublin;
types = (
locality,
political
);
},
{
"long_name" = "Dublin 1";
"short_name" = "Dublin 1";
types = (
"postal_town"
);
},
{
"long_name" = "Dublin City";
"short_name" = "Dublin City";
types = (
"administrative_area_level_2",
political
);
},
{
"long_name" = Dublin;
"short_name" = Dublin;
types = (
"administrative_area_level_1",
political
);
},
{
"long_name" = Ireland;
"short_name" = IE;
types = (
country,
political
);
}
);
"formatted_address" = "National College of Ireland, Mayor Street Lower, International Financial Services Centre, Dublin 1, Ireland";
geometry = {
bounds = {
northeast = {
lat = "53.34914449999999";
lng = "-6.2420244";
};
southwest = {
lat = "53.3484171";
lng = "-6.2435218";
};
};
location = {
lat = "53.3487808";
lng = "-6.242773100000001";
};
"location_type" = ROOFTOP;
viewport = {
northeast = {
lat = "53.3501297802915";
lng = "-6.241424119708499";
};
southwest = {
lat = "53.3474318197085";
lng = "-6.244122080291502";
};
};
};
"place_id" = "ChIJ7fLaG40OZ0gRJsCRPZA1_iA";
types = (
premise
);
},
{
"address_components" = (
{
"long_name" = "Excise Walk";
"short_name" = "Excise Walk";
types = (
route
);
},
{
"long_name" = Dublin;
"short_name" = Dublin;
types = (
locality,
political
);
},
{
"long_name" = "Dublin City";
"short_name" = "Dublin City";
types = (
"administrative_area_level_2",
political
);
},
{
"long_name" = Dublin;
"short_name" = Dublin;
types = (
"administrative_area_level_1",
political
);
},
{
"long_name" = Ireland;
"short_name" = IE;
types = (
country,
political
);
}
);
"formatted_address" = "Excise Walk, Dublin, Ireland";
geometry = {
bounds = {
northeast = {
lat = "53.3492304";
lng = "-6.2434942";
};
southwest = {
lat = "53.3484317";
lng = "-6.243662899999999";
};
};
location = {
lat = "53.34883110000001";
lng = "-6.243578599999999";
};
"location_type" = "GEOMETRIC_CENTER";
viewport = {
northeast = {
lat = "53.3501800302915";
lng = "-6.242229569708497";
};
southwest = {
lat = "53.3474820697085";
lng = "-6.244927530291502";
};
};
};
"place_id" = "ChIJG_rIHI0OZ0gRHTz7pFf9r7o";
types = (
route
);
},
{
"address_components" = (
{
"long_name" = "North Dock";
"short_name" = "North Dock";
types = (
neighborhood,
political
);
},
{
"long_name" = Dublin;
"short_name" = Dublin;
types = (
locality,
political
);
},
{
"long_name" = "Dublin City";
"short_name" = "Dublin City";
types = (
"administrative_area_level_2",
political
);
},
{
"long_name" = Dublin;
"short_name" = Dublin;
types = (
"administrative_area_level_1",
political
);
},
{
"long_name" = Ireland;
"short_name" = IE;
types = (
country,
political
);
}
);
"formatted_address" = "North Dock, Dublin, Ireland";
geometry = {
bounds = {
northeast = {
lat = "53.3608191";
lng = "-6.1894282";
};
southwest = {
lat = "53.34522";
lng = "-6.2549399";
};
};
location = {
lat = "53.3497493";
lng = "-6.2306567";
};
"location_type" = APPROXIMATE;
viewport = {
northeast = {
lat = "53.3608191";
lng = "-6.1894282";
};
southwest = {
lat = "53.34522";
lng = "-6.2549399";
};
};
};
"place_id" = "ChIJr9h9M_oOZ0gRrycKzPZj46A";
types = (
neighborhood,
political
);
},
{
"address_components" = (
{
"long_name" = "Dublin Northside";
"short_name" = "Dublin Northside";
types = (
neighborhood,
political
);
},
{
"long_name" = Ashtown;
"short_name" = Ashtown;
types = (
"sublocality_level_1",
sublocality,
political
);
},
{
"long_name" = Dublin;
"short_name" = Dublin;
types = (
locality,
political
);
},
{
"long_name" = "Dublin City";
"short_name" = "Dublin City";
types = (
"administrative_area_level_2",
political
);
},
{
"long_name" = Dublin;
"short_name" = Dublin;
types = (
"administrative_area_level_1",
political
);
},
{
"long_name" = Ireland;
"short_name" = IE;
types = (
country,
political
);
}
);
"formatted_address" = "Dublin Northside, Ashtown, Dublin, Ireland";
geometry = {
bounds = {
northeast = {
lat = "53.4088667";
lng = "-6.0358715";
};
southwest = {
lat = "53.34558130000001";
lng = "-6.381769299999999";
};
};
location = {
lat = "53.3815507";
lng = "-6.1922052";
};
"location_type" = APPROXIMATE;
viewport = {
northeast = {
lat = "53.4088667";
lng = "-6.0358715";
};
southwest = {
lat = "53.34558130000001";
lng = "-6.381769299999999";
};
};
};
"place_id" = ChIJvUSkJbcPZ0gRyXXPH9MSQwk;
types = (
neighborhood,
political
);
},
{
"address_components" = (
{
"long_name" = Dublin;
"short_name" = Dublin;
types = (
locality,
political
);
},
{
"long_name" = "Dublin City";
"short_name" = "Dublin City";
types = (
"administrative_area_level_2",
political
);
},
{
"long_name" = Dublin;
"short_name" = Dublin;
types = (
"administrative_area_level_1",
political
);
},
{
"long_name" = Ireland;
"short_name" = IE;
types = (
country,
political
);
}
);
"formatted_address" = "Dublin, Ireland";
geometry = {
bounds = {
northeast = {
lat = "53.42521010000001";
lng = "-6.0439235";
};
southwest = {
lat = "53.22343009999999";
lng = "-6.4474846";
};
};
location = {
lat = "53.3498053";
lng = "-6.2603097";
};
"location_type" = APPROXIMATE;
viewport = {
northeast = {
lat = "53.42521010000001";
lng = "-6.0439235";
};
southwest = {
lat = "53.22343009999999";
lng = "-6.4474846";
};
};
};
"place_id" = ChIJL6wn6oAOZ0gRoHExl6nHAAo;
types = (
locality,
political
);
},
{
"address_components" = (
{
"long_name" = "Dublin City";
"short_name" = "Dublin City";
types = (
"administrative_area_level_2",
political
);
},
{
"long_name" = Dublin;
"short_name" = Dublin;
types = (
"administrative_area_level_1",
political
);
},
{
"long_name" = Ireland;
"short_name" = IE;
types = (
country,
political
);
}
);
"formatted_address" = "Dublin City, Co. Dublin, Ireland";
geometry = {
bounds = {
northeast = {
lat = "53.4111566";
lng = "-6.1131916";
};
southwest = {
lat = "53.2988569";
lng = "-6.3870807";
};
};
location = {
lat = "53.3603142";
lng = "-6.315054200000001";
};
"location_type" = APPROXIMATE;
viewport = {
northeast = {
lat = "53.4111566";
lng = "-6.1131916";
};
southwest = {
lat = "53.2988569";
lng = "-6.3870807";
};
};
};
"place_id" = ChIJv2RI7foRZ0gRwAKA8azHAAM;
types = (
"administrative_area_level_2",
political
);
},
{
"address_components" = (
{
"long_name" = Dublin;
"short_name" = Dublin;
types = (
"administrative_area_level_1",
political
);
},
{
"long_name" = Ireland;
"short_name" = IE;
types = (
country,
political
);
}
);
"formatted_address" = "Co. Dublin, Ireland";
geometry = {
bounds = {
northeast = {
lat = "53.6347257";
lng = "-5.9962748";
};
southwest = {
lat = "53.1781971";
lng = "-6.5468798";
};
};
location = {
lat = "53.3824769";
lng = "-6.3133674";
};
"location_type" = APPROXIMATE;
viewport = {
northeast = {
lat = "53.6347257";
lng = "-5.9962748";
};
southwest = {
lat = "53.1781971";
lng = "-6.5468798";
};
};
};
"place_id" = ChIJv2RI7foRZ0gRwAKA8azHABg;
types = (
"administrative_area_level_1",
political
);
},
{
"address_components" = (
{
"long_name" = Ireland;
"short_name" = IE;
types = (
country,
political
);
}
);
"formatted_address" = Ireland;
geometry = {
bounds = {
northeast = {
lat = "55.4351345";
lng = "-5.994700099999999";
};
southwest = {
lat = "51.4199312";
lng = "-10.66958";
};
};
location = {
lat = "53.41291";
lng = "-8.24389";
};
"location_type" = APPROXIMATE;
viewport = {
northeast = {
lat = "55.4351345";
lng = "-5.99471";
};
southwest = {
lat = "51.4199312";
lng = "-10.6694501";
};
};
};
"place_id" = "ChIJ-ydAXOS6WUgRCPTbzjQSfM8";
types = (
country,
political
);
}
);
status = OK; }
Your for loop is going over all of the address_components and assigning their values to your longName and shortName variables, thus when the loop finishes only the last retrieved values in the array will be stored. If you only want the first set then you probably want to do something like this:
if ( addressComponents.count > 0 ) {
NSDictionary *componentDict = addressComponents[0];
longName = componentDict[#"long_name"];
shortName = componentDict[#"short_name"];
}
//The address_components array contains many dictionaries,
//we loop through each dictionary and check the types array
for (NSDictionary *addressComponentDictionary in addressComponents) {
longName = addressComponentDictionary[#"long_name"];
shortName = addressComponentDictionary[#"short_name"];
}
as Larme said your problem is here, instead of longName and shortName.
Create 2 arrays:
NSMutableArray *longNameArray = [[NSMutableArray alloc] init]
NSMutableArray *shortNameArray = [[NSMutableArray alloc] init]
for (NSDictionary *addressComponentDictionary in addressComponents) {
[longNameArray addObject:addressComponentDictionary[#"long_name"]];
[shortNameArray addObject:addressComponentDictionary[#"short_name"]];
}
This will give you the full list of elements found in the request.
Update - Get only the first element
If you need only the first element, get the first item from array
//This check will ensure that the array is not empty
if (addressComponents.count > 0) {
NSDictionary *addressComponentDictionary = addressComponents[0];
longName = addressComponentDictionary[#"long_name"];
shortName = addressComponentDictionary[#"short_name"];
}

Weird issue with valueForKeyPath:#"#max.totalSteps", is this bug in iOS platform code?

I spent few hours in debugging this issue, and hence posting this, in case if you have expert advice/clue.
Here is the code where the problem is:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"plot" ofType:#"json"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];
NSInteger minSteps = [dict[#"steps"] valueForKeyPath:#"#min.totalSteps"];
NSInteger maxSteps = [dict[#"steps"] valueForKeyPath:#"#max.totalSteps"];
NSLog(#"Min Steps = %#", minSteps);
NSLog(#"Max Steps = %#", maxSteps);
JSON contents provided below, the above code expected to produce minSteps to be "0" (zero), and maxSteps to be "710".
However above code producing the following out out.
Min Steps = 0
Max Steps = 90
Why is max value found to be "90"??
Any clues are appreciated?
Contents of plot.json file:
{
steps = (
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 75;
},
{
totalSteps = 184;
},
{
totalSteps = 113;
},
{
totalSteps = 90;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 11;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 386;
},
{
totalSteps = 282;
},
{
totalSteps = 22;
},
{
totalSteps = 42;
},
{
totalSteps = 710;
},
{
totalSteps = 200;
},
{
totalSteps = 0;
},
{
totalSteps = 111;
},
{
totalSteps = 12;
},
{
totalSteps = 0;
},
{
totalSteps = 49;
},
{
totalSteps = 67;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 104;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 55;
}
);
}
Providing Debugger output per request:
PS: Sorry for the length of the JSON, with duplication, as this is close to the real time data.
(lldb) po dict
{
steps = (
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 75;
},
{
totalSteps = 184;
},
{
totalSteps = 113;
},
{
totalSteps = 90;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 11;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 386;
},
{
totalSteps = 282;
},
{
totalSteps = 22;
},
{
totalSteps = 42;
},
{
totalSteps = 710;
},
{
totalSteps = 200;
},
{
totalSteps = 0;
},
{
totalSteps = 111;
},
{
totalSteps = 12;
},
{
totalSteps = 0;
},
{
totalSteps = 49;
},
{
totalSteps = 67;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 104;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 0;
},
{
totalSteps = 55;
}
);
}
(lldb) po [dict[#"steps"] valueForKeyPath:#"#min.totalSteps"];
0
(lldb) po [dict[#"steps"] valueForKeyPath:#"#max.totalSteps"];
90
(lldb) po minSteps
0
(lldb) po maxSteps
90
(lldb)
#Matt, Thanks again. I have also found solution that was working:
For other benefit I am providing the code that works well.
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"plot" ofType:#"json"];
NSError *jsonParsingError = nil;
NSMutableDictionary *responseObject = [NSJSONSerialization
JSONObjectWithData:[NSData dataWithContentsOfFile:filePath]
options:NSJSONReadingMutableContainers error:&jsonParsingError];
NSInteger minSteps = [dict[#"steps"] valueForKeyPath:#"#min.totalSteps"];
NSInteger maxSteps = [dict[#"steps"] valueForKeyPath:#"#max.totalSteps"];
NSLog(#"Min Steps = %#", minSteps);
NSLog(#"Max Steps = %#", maxSteps);
However, please note, provided JSON structure is not quite correct as well, requires few modification, to be accepted by NSJSONSerialization parser.
Here is modified file with acceptable format:
{
“steps” : (
{
“totalSteps” : 0,
},
...
If you see problem like this:
(lldb) po jsonParsingError
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (No string key for value in object around character 6.) UserInfo=0x109875d50 {NSDebugDescription=No string key for value in object around character 6.}
Please check validity of JSON formatting. And make sure your file is not in RTF format, etc...
It is because when your file is turned into a dictionary, steps is ending up as an array of dictionaries where totalSteps is the key and the value is a string. From a string point of view, "90" is the largest (because it begins with the letter "9", which comes later in the "alphabet" than any other digit; the digits, as characters, run from "0" to "9").
And the reason for that is that you are reading the file as a .plist - you are not reading it as JSON at all. [NSDictionary dictionaryWithContentsOfFile:filePath] interprets the file as a .plist. If you want it interpreted as JSON, read it as an NSData and then use the JSON methods (NSJSONSerialization) to convert it to Cocoa objects.
One must not imagine that the file will be read as JSON merely because its name ends in "json". dictionaryWithContentsOfFile: does only one thing: it reads a file as a .plist. Your file is just making it in the door, because it can be understood as an "old-style property list": see https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/PropertyLists/OldStylePlists/OldStylePLists.html#//apple_ref/doc/uid/20001012-BBCBDBJE. There are no numbers in an old-style property list; only strings. In a way, you were unlucky; it would have been better for you if the attempt to read the file as a property list had failed! But unfortunately NSDictionary was able to interpret it as an old-style property list.

JSON that contains an array to NSString

How do I extract each address from the NSDictionary I instantiated (below), so I can assign to various NSString objects?
I have the following JSON:
{
"Address":[
{
"$":{
"ID":"0"
},
"Address2":[
"10 Smith RD"
],
"City":[
"Mapleville"
],
"State":[
"NJ"
],
"Zip5":[
"90210"
],
"Zip4":[
"764"
]
},
{
"$":{
"ID":"1"
},
"Address2":[
"32 Hog CT"
],
"City":[
"New York City"
],
"State":[
"NY"
],
"Zip5":[
"90210"
],
"Zip4":[
"1390"
]
}
]
}
cData (below) came from the JSON above.
I did the following to convert to a NSDictionary:
NSDictionary* dictionary2 = [NSJSONSerialization JSONObjectWithData:cData options:NSJSONReadingAllowFragments error:nil];
once I did a:
NSLog(#"%#", dictionary2);
output from NSLog:
{
Address = (
{
"$" = {
ID = 0;
};
Address2 = (
"10 Smith RD"
);
City = (
"Mapleville"
);
State = (
NJ
);
Zip4 = (
7642
);
Zip5 = (
90210
);
},
{
"$" = {
ID = 1;
};
Address2 = (
"32 Hog CT"
);
City = (
"New York City"
);
State = (
NY
);
Zip4 = (
1390
);
Zip5 = (
90210
);
}
);
}
Just follow the structure.
NSDictionary* dictionary2 = [NSJSONSerialization ...
NSArray *addresses = dictionary2[#"Address"];
for (NSDictionary *addressData in addresses) {
NSString *address2 = addressData[#"Address2"];
NSString *city = addressData[#"City"];
// and the rest as needed
}

Resources