NSDictionary seems to have multiple values for one key, how do I access the individual values? - objective-c

I am using ADAL to authenticate to Azure AD, it returns an dictionary with claims, one of the keys is the groups that a user is a member of,
The full dictionary looks like this
2017-06-05 17:40:58.712 NWMobileTill[46676:3282242] userInfoStore Item all {
alg = none;
amr = (
pwd
);
aud = "042a00fc-b832-411f-xxxxxxxx";
exp = xxxxxxx;
groups = (
"xxx-9725-43f6-a502-xxxxx",
"38c5b3af-xxx-4b38-b180-xxxx"
);
iat = xxx;
ipaddr = "xx";
iss = "https://xxxxxxx/xxx-d61d-xxx-a949-0cb72eff23be/";
name = "POS Test";
nbf = xxx;
oid = "c44f91f2-xx-40bb-9624-xxx";
platf = 2;
sub = "xxxxx";
tid = "b5154a9e-xxx-4d55-a949-xxx";
typ = JWT;
"unique_name" = "nwpos#xxxxxx";
upn = "nwpos#xxxx";
ver = "1.0";
}
when I access they values in the NSDIctionary using the objectForKey as below
NSDictionary *jongel = [result.tokenCacheStoreItem.userInformation.allClaims objectForKey:#"groups"];
I get multiple entries like this,
2017-06-05 17:40:58.712 NWMobileTill[46676:3282242] Groups are (
"xx-9725-43f6-a502-xx",
"xx-d0dc-4b38-xx-17555db6f626"
)
I am confused that I can get multiple values, how do I access each individual value? I happen to assign this to an NSDictionary but I don't know if that is right. How can I get to those individual entries?

The groups is an array, so when you do the [objectForKey...] call it returns the data as expected. I would assign the returned object to an NSArray as it should always be returned as from that data model as an array, even if it only contains one element.
If you're more curious you can add a breakpoint in and examine it in a debugger, the [objectForKey...] method does some interesting behaviour around type conversions and sometimes the results may surprise you.

Related

Add business objects to contacts

I've successfully created a contact in the SAP IS-U (Release 618) system using the function: BCONTACT_CREATE
EDIT:
Since this question was voted "close" for being "too broad" - here's some very specific code:
DATA:
ls_contact TYPE bpc01_bcontact_auto,
ls_contact_properties TYPE bcont,
lv_contact_text TYPE string,
lv_bp TYPE bu_partner,
lv_bpcontact_id TYPE ct_contact,
lv_no_dialog TYPE flag VALUE abap_true,
lv_repid TYPE syst-repid.
* Main logic
lv_contact_text = 'Test'.
lv_bp = '0010000062'.
ls_contact_properties-cclass = '0003'.
ls_contact_properties-activity = '0001'.
ls_contact_properties-f_coming = '3'.
* Mapping
*--------------------------------------------------------------------*
ls_contact-notice-line = lv_contact_text.
ls_contact-bcontd = ls_contact_properties.
* set flag to use auto data
ls_contact-bcontd_use = abap_true.
lv_repid = sy-repid.
CALL FUNCTION 'BCONTACT_CREATE'
EXPORTING
x_no_dialog = lv_no_dialog
x_auto = ls_contact
x_prgcontext = lv_repid
x_partner = lv_bp
IMPORTING
y_new_bpcontact = lv_bpcontact_id
EXCEPTIONS
existing = 1
foreign_lock = 2
number_error = 3
general_fault = 4
input_error = 5
not_authorized = 6
OTHERS = 7.
When I open the created contact in the BCT2 transaction I see the nothing under Business-Objects:
How can I programmatically add a business object to a contact, so that it is displayed here like this?
I found it a solution!
First create variables (a table and a structure for filling the table) for your business objects that you want to add (I saw some code that had a limit of 5 so I just set that too to be safe):
lt_business_objs TYPE TABLE OF bpc_obj INITIAL SIZE 5,
ls_business_obj TYPE bpc_obj,
Next append your object, in this example I'm just appending one:
* Append business objects
*--------------------------------------------------------------------*
ls_business_obj-objkey = 'The value here may be your business object input value'.
ls_business_obj-objrole = 'DEFAULT'. "Don't know what this is for...
ls_business_obj-objtype = 'OBJECT_NAME'. "Name of your business object - seen in table TOJTB
APPEND ls_business_obj TO lt_business_objs.
And lastly put the object list into the contact structure:
ls_contact-iobjects = lt_business_objs.

Data class .copy only if nullable parameter is not null

I have a Front-End application that sends me Data to update my User (updatedUser). Since I don't want to send the whole Userdata, I'm only sending the data that has changed. Now I want to Update my Userdata with the changes provided, so I'd like to know if there is a more elegant way to do this than just a list of ifs/lets. I'm quite new to kotlin, so don't expect too much from me^^
Not so elegant way:
changeData.firstname?.let { updatedUser.firstname = it }
changeData.lastname?.let { updatedUser.lastname = it }
...
Expected (doesn't work - type mismatch):
updatedUser.copy(
firstname = changeData?.firstname,
lastname = changeData?.lastname,
...)
the reason you get a type mismatch is There is a string type and a string nullable type
var variableName:String = "myData" // if you want a non nullable
var variableName:String? = "myDataThatCouldBeNull" // if you want a string that could be null

Why can I not use Continuation when using a proxy class to access MS CRM 2013?

So I have a standard service reference proxy calss for MS CRM 2013 (i.e. right-click add reference etc...) I then found the limitation that CRM data calls limit to 50 results and I wanted to get the full list of results. I found two methods, one looks more correct, but doesn't seem to work. I was wondering why it didn't and/or if there was something I'm doing incorrectly.
Basic setup and process
crmService = new CrmServiceReference.MyContext(new Uri(crmWebServicesUrl));
crmService.Credentials = System.Net.CredentialCache.DefaultCredentials;
var accountAnnotations = crmService.AccountSet.Where(a => a.AccountNumber = accountNumber).Select(a => a.Account_Annotation).FirstOrDefault();
Using Continuation (something I want to work, but looks like it doesn't)
while (accountAnnotations.Continuation != null)
{
accountAnnotations.Load(crmService.Execute<Annotation>(accountAnnotations.Continuation.NextLinkUri));
}
using that method .Continuation is always null and accountAnnotations.Count is always 50 (but there are more than 50 records)
After struggling with .Continutation for a while I've come up with the following alternative method (but it seems "not good")
var accountAnnotationData = accountAnnotations.ToList();
var accountAnnotationFinal = accountAnnotations.ToList();
var index = 1;
while (accountAnnotationData.Count == 50)
{
accountAnnotationData = (from a in crmService.AnnotationSet
where a.ObjectId.Id == accountAnnotationData.First().ObjectId.Id
select a).Skip(50 * index).ToList();
accountAnnotationFinal = accountAnnotationFinal.Union(accountAnnotationData).ToList();
index++;
}
So the second method seems to work, but for any number of reasons it doesn't seem like the best. Is there a reason .Continuation is always null? Is there some setup step I'm missing or some nice way to do this?
The way to get the records from CRM is to use paging here is an example with a query expression but you can also use fetchXML if you want
// Query using the paging cookie.
// Define the paging attributes.
// The number of records per page to retrieve.
int fetchCount = 3;
// Initialize the page number.
int pageNumber = 1;
// Initialize the number of records.
int recordCount = 0;
// Define the condition expression for retrieving records.
ConditionExpression pagecondition = new ConditionExpression();
pagecondition.AttributeName = "address1_stateorprovince";
pagecondition.Operator = ConditionOperator.Equal;
pagecondition.Values.Add("WA");
// Define the order expression to retrieve the records.
OrderExpression order = new OrderExpression();
order.AttributeName = "name";
order.OrderType = OrderType.Ascending;
// Create the query expression and add condition.
QueryExpression pagequery = new QueryExpression();
pagequery.EntityName = "account";
pagequery.Criteria.AddCondition(pagecondition);
pagequery.Orders.Add(order);
pagequery.ColumnSet.AddColumns("name", "address1_stateorprovince", "emailaddress1", "accountid");
// Assign the pageinfo properties to the query expression.
pagequery.PageInfo = new PagingInfo();
pagequery.PageInfo.Count = fetchCount;
pagequery.PageInfo.PageNumber = pageNumber;
// The current paging cookie. When retrieving the first page,
// pagingCookie should be null.
pagequery.PageInfo.PagingCookie = null;
Console.WriteLine("#\tAccount Name\t\t\tEmail Address");while (true)
{
// Retrieve the page.
EntityCollection results = _serviceProxy.RetrieveMultiple(pagequery);
if (results.Entities != null)
{
// Retrieve all records from the result set.
foreach (Account acct in results.Entities)
{
Console.WriteLine("{0}.\t{1}\t\t{2}",
++recordCount,
acct.EMailAddress1,
acct.Name);
}
}
// Check for more records, if it returns true.
if (results.MoreRecords)
{
// Increment the page number to retrieve the next page.
pagequery.PageInfo.PageNumber++;
// Set the paging cookie to the paging cookie returned from current results.
pagequery.PageInfo.PagingCookie = results.PagingCookie;
}
else
{
// If no more records are in the result nodes, exit the loop.
break;
}
}

MapKit: How to retrieve apartment number from the return value of MKLocalSearchResponse

I searched thoroughly online but couldn't find any discussion about this:
The MKLocalSearchResponse object returned from a MapKit search is a collection of MKMapItem, which has the information of search results, e.g. City, state, country.
A single MKMapItem looks like this(from Xcode quick look of the object):
"Name: ADVANCED SOLUTIONS ADDICTION MANAGEMENT CurrentLocation: 0 Place: <GEOPlace: 0x9b2db90> {
address = {
formattedAddressLine = (
\"205 W Crestway Ave\",
\"Unit 200\",
\"Derby, KS 67037-1850\",
\"United States\"
);
structuredAddress = {
administrativeArea = Kansas;
administrativeAreaCode = KS;
country = \"United States\";
countryCode = US;
dependentLocality = (
Derby,
Rockford
);
fullThoroughfare = \"205 W Crestway Ave\";
geoId = (
);
locality = Derby;
postCode = 67037;
postCodeExtension = 1850;
postCodeFull = \"67037-1850\";
subAdministrativeArea = Sedgwick;
subLocality = Rockford;
subPremise = (
{
name = 200;
type = 0;
}
);
subThoroughfare = 205;
thoroughfare = \"W Crestway Ave\";
};
};
}"
I was able to retrieve all the information I needed, except the Apartment number. It's contained in the "subPremise" part, which I don't know how to retrieve.
You may suggest me to retrieve it from the "formattedAddressLines", which I have access to, but for some results, that property is empty, so I cannot rely on it.
I've also tried the "addressDictionary" property, it has all the necessary information except apartment number, which is very unthoughtful to me.
mapItem.placemark.subThoroughfare. Note that it may be empty

NSMutable array containing NSDictionary to server

I'm creating a small project where I want to post a NSMutableArray containing multiple NSDictionarys to a server. The thing is that the Array is dynamic. I don't know how many dictionaries it will contain. This is the layout of the Mutablearray:
(
{
Category = Music;
Description = "Detta \U00e4r mitt quiz!";
Difficulty = 1;
Language = Swedish;
Title = "Mitt Quiz";
},
{
QuestionNr1 = {
Question = "Vilken stad bor jag i?";
RightAnswer = Uppsala;
WrongAnswer1 = Stockholm;
WrongAnswer2 = "Ume\U00e5";
WrongAnswer3 = Visby;
};
QuestionNr2 = {
Question = "Vilken stad bor jag inte i?";
RightAnswer = Uppsala;
WrongAnswer1 = Stockholm;
WrongAnswer2 = "Ume\U00e5";
WrongAnswer3 = Visby;
};
}
)
I now want to post this to PHP/MYSQL-Server. I could do this by Splitting up the array to it's string components and concatenating a string to a URL containing all it's variables. But that wouldn't work if I'm not knowing how many Questions/Dictionarys the array will contain. Plus that it feels wrong building this LONG url for the request.
Is there any other way, using JSON for example that makes posting this structure to my php-server easy?
Thanks!
This is exactly what JSON was designed to do. See the documentation for NSJSONSerialization.