Obj-C: NSString crashes with error "Terminating app due to uncaught exception NSInvalidArgumentException"? - ios

For some reason, the following line in my app is returned NULL, and thus, crashes my app:
NSString *address = [session user][#"field_street_address"][#"und"][0][#"safe_value"];
Which, I don't understand, as my console below states that data is returned for field_street_address. Is there something wrong with that line that I'm just not seeing? I've been staring at this for a while and I feel like I'm missing something obvious.
ViewController.m
NSDictionary *userDictInfo = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:#"diosSession"]];
DIOSSession *session = [DIOSSession sharedSession];
[session setUser:userDictInfo];
[session user];
NSString *address = [session user][#"field_street_address"][#"und"][0][#"safe_value"];
Console ([session user] log):
2017-10-06 14:06:22.226970-0700 app[828:193706] {
sessid = "DRY0fOXtO_FZOIeowFVVq8oalaFnKSe";
"session_name" = SESS2bb8896be0f16543ff3c6a;
token = giCdHBuw967IaSxDB34m0Evzf1HI3DIK6;
user = {
access = 1507310936;
created = 1459875505;
data = {
"ckeditor_auto_lang" = t;
"ckeditor_default" = t;
"ckeditor_lang" = en;
"ckeditor_show_toggle" = t;
"ckeditor_width" = "100%";
};
"field_address" = {
und = (
{
format = "<null>";
"safe_value" = "1325 Fake Street";
value = "1325 Fake Street";
}
);
};
"field_childrenunder" = {
und = (
{
format = "<null>";
"safe_value" = No;
value = No;
}
);
};
"field_city" = {
und = (
{
format = "<null>";
"safe_value" = Van;
value = Van;
}
);
};
"field_emergency_facility" = {
und = (
{
format = "<null>";
"safe_value" = Yes;
value = Yes;
}
);
};
"field_first_name" = {
und = (
{
format = "<null>";
"safe_value" = Brittany;
value = Brittany;
}
);
};
"field_last_name" = {
und = (
{
format = "<null>";
"safe_value" = B;
value = B;
}
);
};
"field_phonenumber" = {
und = (
{
format = "<null>";
"safe_value" = 2369893091;
value = 2369893091;
}
);
};
"field_photo_path" = {
und = (
{
format = "<null>";
"safe_value" = "sites/default/files/stored/1507092784.jpg";
value = "sites/default/files/stored/1507092784.jpg";
}
);
};
"field_points_balance" = {
und = (
{
format = "<null>";
"safe_value" = 12;
value = 12;
}
);
};
"field_postal_code" = {
und = (
{
format = "<null>";
"safe_value" = 000000;
value = 000000;
}
);
};
"field_private_message_notify" = {
und = (
{
value = 1;
}
);
};
"field_profile_photo" = {
und = (
{
alt = "";
fid = 237;
"field_file_image_alt_text" = (
);
"field_file_image_title_text" = (
);
filemime = "image/jpeg";
filename = "1507092784.jpg";
filesize = 16084;
height = 296;
metadata = {
height = 296;
width = 300;
};
"rdf_mapping" = (
);
status = 1;
timestamp = 1507108254;
title = "";
type = image;
uid = 47;
uri = "public://stored/1507092784.jpg";
width = 300;
}
);
};
"field_property_type" = {
und = (
{
format = "<null>";
"safe_value" = House;
value = House;
}
);
};
"field_province" = {
und = (
{
format = "<null>";
"safe_value" = BC;
value = BC;
}
);
};
"field_special_skills" = {
und = (
{
format = "<null>";
"safe_value" = "Oral medication";
value = "Oral medication";
}
);
};
"field_star_rating" = {
und = (
{
format = "<null>";
"safe_value" = 1;
value = 1;
}
);
};
"field_street_address" = {
und = (
{
format = "<null>";
"safe_value" = "1325 Fake Street";
value = "1325 Fake Street";
}
);
};
"field_supervision" = {
und = (
{
format = "<null>";
"safe_value" = No;
value = No;
}
);
};
"field_userbio" = {
und = (
{
format = "<null>";
"safe_value" = "Hi my name is Brittany.";
value = "Hi my name is Brittany.";
}
);
};
language = "";
login = 1507320712;
mail = "brittany-b#shaw.ca";
name = Brittany;
picture = "<null>";
"rdf_mapping" = {
homepage = {
predicates = (
"foaf:page"
);
type = rel;
};
name = {
predicates = (
"foaf:name"
);
};
rdftype = (
"sioc:UserAccount"
);
};
roles = {
2 = "authenticated user";
};
signature = "";
"signature_format" = "filtered_html";
status = 1;
theme = "";
timezone = UTC;
uid = 47;
};
}

If the log output in your question is from logging [session user] then you need to first access the #"user" key.
NSString *address = [session user][#"user"][#"field_street_address"][#"und"][0][#"safe_value"];
BTW - for issues like this it really helps to break down the code:
NSDictionary *sessionUser = [session user];
NSDictionary *user = sessionUser[#"user"];
NSDicitonary *streetAddr = user[#"field_street_address"];
// etc.
Then you can see where you start getting nil and look at the previous results to determine where things are going wrong.

Related

how to extract Elements in NSMutable Dictionary Array

my server sends me an response in hierarchy way i do no how to grab the value from that
here my sample response :
{
id = "-1";
result = (
{
keyname = "Warranty Expiry Date";
value = "06-14-2017";
}
);
},
{
id = "-1";
result = (
{
keyname = Owner;
value = "";
}
);
},
{
id = "-1";
result = (
{
keyname = Model;
value = "";
}
);
},
{
id = "-1";
result = (
{
keyname = "Price $";
value = 1000;
}
);
},
{
id = "-1";
result = (
{
keyname = Vendor;
value = "";
}
);
},
{
id = "-1";
result = (
{
keyname = "Purchase Date";
value = "";
}
);
}
)
here is sample Java code how they extracted the values
Hashtable htAttrs = (Hashtable) editedAttrHash.get(assItmId);
if (htAttrs != null) {
Set<String> keys = htAttrs.keySet();
for (String deAttr : keys) {
xmlSerializer.startTag("", "attribute");
xmlSerializer.attribute("", "keyname", deAttr);
xmlSerializer.attribute("", "value",
htAttrs.get(deAttr).toString());
xmlSerializer.endTag("", "attribute");
}
}
i need to do no how to from this structure past one day i am blocked here anyone can fix my problem
How to extract values from nsmutablearray?If it is really nsmutablearray object, then extract the values is simple.
For example, to get id of first element:
NSString firstId = response[0][#"id"]
But it seems, like for now, it is just a json object. So, you should serialize it to nsarray object first.

EventEmiter pausing and restarting

I have the following code
.html
<input
type = "text"
#raceCtrl = "ngForm"
[ngFormControl] = "raceForm.controls['raceCtrl']"
[(ngModel)] = "race.race"
(input) = "inputHandler()"
(mousemove) = "inputHandler()"
class = "mdl-textfield__input"
id = "race">
<input
type = "text"
#ethnicGroupCtrl = "ngForm"
[ngFormControl] = "raceForm.controls['ethnicGroupCtrl']"
[(ngModel)] = "race.ethnicGroup"
(input) = "inputHandler()"
(mousemove) = "inputHandler()"
class = "mdl-textfield__input"
id = "ethnicGroup">
.dart
RaceComponent( FormBuilder fb, ModelService modelSrvc ) {
raceForm = fb.group( {
'raceCtrl': ['', CustomValidators.requiredNounValidator( 2 )],
'ethnicGroupCtrl': ['', CustomValidators.optionalNounValidator( 2 )]
} );
_raceCtrl = raceForm.controls['raceCtrl'];
race = modelSrvc.getRace( );
inputHandler( );
}
void inputHandler( ) {
if ( raceForm.valid &&
!isEmpty( _raceCtrl.value ) ) {
isFormValid = true;
}
else {
isFormValid = false;
}
race.isValid = isFormValid;
}
onSubmit( ) {
isSubmitted = true;
if ( isFormValid ) {
raceEmitter.emit( {'topic': topic, 'race': race} );
}
else {
raceEmitter.emit( {'topic': topic, 'race': new Race( )} );
}
raceMap = new JsonObject.fromJsonString( encode( race ) );
The form submits as expected. But from then onwards the stream remains open and even when ethinicGroup has errors the error is propagated by the stream to storage. Is it possible to pause and unpause the stream depending on the validity of the form to prevent the erroneous data from being propagated.

How to access NSArray inside a NSDictionary while parsing a xml?

I am using XMLReader to parse a xml response. I have converted the xml to a NSDictionary. Now i want to access the array named 'cTrx' inside the NSDictionary. This is my NSDictionary:
{
Envelope = {
Body = {
fRetrieveTransactionsforReversalResponse = {
fRetrieveTransactionsforReversalResult = {
pCellularNumber = {
text = 0825669995;
};
pResponseCode = {
text = 00;
};
pResponseMessage = {
text = Approved;
};
pTrx = {
cTrx = (
{
pCardHolderName = {
};
pReference = {
text = 140826121114;
};
pTransactionAccount = {
text = "364241****0016";
};
pTransactionDate = {
text = "8/26/2014 12:11:18 PM";
};
pTransactionID = {
text = 23;
};
},
{
"xsi:nil" = true;
}
);
};
};
};
};
};
}
Thanks.
You can access dictionary and array values with square brackets:
val = dictionary[#"key"];
item = array[i];
It works for nested dictionaries as well:
NSDictionary *nested = #{#"dict_key": dictionary};
dictionaryVal = nexted[#"dict_key"][#"key"];
So, in your case, you could access cTrx array in next way:
NSArray *cTrx = yourDictionary[#"Envelope"][#"Body"][#"fRetrieveTransactionsforReversalResponse"][#"pTrx"][#"cTrx"];
Long string of code :)

Facebook Graph API - Status updates

I am currently using the Graph API to retrieve the News Feed for a user. However, some Posts of type "update" do not return any info on the original post e.g. in the result extract posted below, the story is "Joubert Nel commented on a link", but there is no info included as to what the link is, or the picture that goes with it. How can I obtain this info please?
actions = (
{
link = "https://www.facebook.com/637106301/posts/520940825396";
name = Comment;
}
);
comments = {
data = (
{
"can_remove" = 0;
"created_time" = "2014-03-15T13:06:43+0000";
from = {
id = 100000194244146;
name = "Laurie Edwards";
};
id = "520940825396_133907";
"like_count" = 2;
message = "Or you could just come home for a while!";
"user_likes" = 0;
},
);
paging = {
cursors = {
after = "MTI=";
before = "MQ==";
};
};
};
"created_time" = "2014-03-15T14:41:56+0000";
from = {
id = 637106301;
name = "Joubert Nel";
};
id = "637106301_520940825396";
likes = {
data = (
{
id = 214500098;
name = "Samuel Freilich";
},
{
);
paging = {
cursors = {
after = "OTUwMTE2OA==";
before = MjE0NTAwMDk4;
};
};
};
privacy = {
value = "";
};
story = "Joubert Nel commented on a link.";
"story_tags" = {
0 = (
{
id = 637106301;
length = 11;
name = "Joubert Nel";
offset = 0;
type = user;
}
);
};
type = status;
"updated_time" = "2014-03-15T14:41:56+0000";
is the link posted by Joubert? or using app you have permission. sounds like you don't have permission to post(link) but u have permission to Joubert activities

NSDictionary recursive loop

I'm using the following function in order to get all the keys and values of the preferences.plist file located in: /private/var/preferences/SystemConfiguration/preferences.plist
- (void)FindKeysAndValuesInPlist:(id)object forKeyNamed:(NSString *)keyName{
if ([object isKindOfClass:[NSDictionary class]])
{
NSLog(#"%#",keyName);
[object enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
[self FindKeysAndValuesInPlist:value forKeyNamed:key];
}];
}
else if ([object isKindOfClass:[NSArray class]])
{
[object enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[self FindKeysAndValuesInPlist:obj forKeyNamed:nil];
}];
}
else
{
NSLog(#"%#.%#", keyName, object);
}
}
An example of the content of preferences.plist:
{
CurrentSet = "/Sets/3B9E7BEB-5558-4497-803B-21B03E6A46C0";
NetworkServices = {
"014226AB-75B7-41CF-9B96-48E82FD6A395" = {
Interface = {
DeviceName = ip4;
Hardware = "com.apple.CommCenter";
Type = "com.apple.CommCenter";
UserDefinedName = "com.apple.CommCenter (ip4)";
};
PrimaryRank = Never;
UserDefinedName = "com.apple.CommCenter (ip4)";
"com.apple.CommCenter" = {
AllowNetworkAccess = 0;
Available = 1;
Version = 11;
};
};
"1C01B561-1A55-4E3B-82FC-CDFF5024F0D2" = {
Interface = {
DeviceName = ip1;
Hardware = "com.apple.CommCenter";
Type = "com.apple.CommCenter";
UserDefinedName = "com.apple.CommCenter (ip1)";
};
UserDefinedName = "com.apple.CommCenter (ip1)";
"com.apple.CommCenter" = {
AllowNetworkAccess = 1;
Available = 1;
SettingsHaveBeenAlteredByPreferences = 1;
Setup = {
apn = "";
password = "";
signature = <7ecb277c ad546563 3ac057fb db40aeaa 939f8c0e e7ae68c2 6e0ff602 77d3868d 18a63059 6c83f66d 46b8af57 d1bf83d0 2655ced6 57d773f4 5c7e733e 923aaa07 39165357 a4ecf270 130276f0 59c7470e 0b61a631 dff04fd1 0bc80cb4 a0dc0a03 96a8ebf0 74c24cdb 84c38239 9f6f7f05 ee032982 8ed1b72d b531405b 09e35f5b>;
"type-mask" = 0;
username = "";
};
Version = 11;
};
};
The problem is that I'm trying to create this kind of output format: MAINKEY.SUBKEY.VALUE
For example:
NetworkServices = {
"014226AB-75B7-41CF-9B96-48E82FD6A395" = {
Interface = {
DeviceName = ip4;
Hardware = "com.apple.CommCenter";
Type = "com.apple.CommCenter";
UserDefinedName = "com.apple.CommCenter (ip4)";
};
Would be: NetworkServices.014226AB-75B7-41CF-9B96-48E82FD6A395.Interface.Hardware.ip4
Iterate over the keys in a dictionary. When the value for a key is another dictionary, push the key onto a stack and recurse on the dictionary value. When the value is a string or number, then concatenate the keys in the stack and print the key path and value.
Code:
+ (void)printDict:(NSDictionary *)dict keyStack:(NSArray *)keys
{
if (keys == nil) {
keys = [NSArray array];
}
for (id key in dict) {
id value = dict[key];
if ([value respondsToSelector:#selector(objectForKey:)]) {
[self printDict:value keyStack:[keys arrayByAddingObject:key]];
} else {
NSLog(#"%# = %#", [[keys arrayByAddingObject:key] componentsJoinedByString:#"."], value);
}
}
}

Resources