Split array object "1;item1" into 1 and item1 - ios

country = (
"3;India",
"4;US",
"5;UK",
)
state = (
"9;3;Uttar Pradesh",
"10;3;Maharashtra",
"11;3;Andaman and Nicobar Islands"
)
city = (
"110;3;10;pune",
"111;3;10;mumbai",
"112;3;10;nasik",
)
I want to show just the country name in dropdown list and want to pass it's Id to web service. If user selects India, I have to pass 3. How can I do this?

As I understand you have an array with strings in it.
If so, you just need to split your strings.
objective-c code
NSArray *country = #[#"3;India", #"4;US", #"5;UK"];
NSString *countryStr = country[0]; // 3; India
NSArray *countryArr = [countryStr componentsSeparatedByString:#";"];
NSString *countryCode = countryArr.firstObject; //3
NSString *countryName = countryArr.lastObject; //India
NSLog(#"%#", countryCode); //3
NSLog(#"%#", countryName); //India
swift code
let country : NSArray = ["3;India", "4;US", "5;UK"]
let countryStr = country[0]
let countryArr : NSArray = countryStr.componentsSeparatedByString(";")
let countryCode = countryArr.firstObject
let countryName = countryArr.lastObject
if let countryCodePr = countryCode {
print(countryCodePr)
}
if let countryNamePr = countryName {
print(countryNamePr)
}

Related

How to retrieve values for key 2 into NSString?

How to retrieve values for key 2 of a NSMutableDictionary into NSString?
My response data is given
response{ 0 = { email = "grover#gmail.com"; firstname = Grover; }; 1 = { email = "hems#gmail.com"; firstname = Gems; }; 2 = { email = "rohit#gmail.com"; firstname = Rohit; }; }
NSDictionary *key2 = response[#"2"];
NSString *email = key2[#"email"];
NSString *firstname = key2[#"firstname"];

Contacts Framework. Access to phones ' values

- (void)viewDidLoad {
[super viewDidLoad];
self.givenName = [[NSMutableArray alloc]init];
NSArray *KeysToFetch = #[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey ];
NSString *containerId = [[[CNContactStore alloc]init]defaultContainerIdentifier];
NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
_contacts = [[[CNContactStore alloc]init]unifiedContactsMatchingPredicate:predicate keysToFetch:KeysToFetch error:nil];
for (CNContact *c in _contacts) {
[_givenName addObject:c.givenName];
NSLog(#"%#", c.phoneNumbers);
}
}
Hi, how can I get access to the 'digits' value? when i do a NSLog of 'phoneNumbers' i get this on console:
2015-10-19 12:33:41.423 testContacts[7064:2001002] (
"<CNLabeledValue: 0x7b72eed0: identifier=A73ABAA0-7698-47D7-A2BD-630E04C0C811, label=_$!<Mobile>!$_, value=<CNPhoneNumber: 0x7b734db0: countryCode=us, digits=8885555512>>",
"<CNLabeledValue: 0x7b72eb80: identifier=BC927A1D-AA98-4E67-82A9-BB5AD09A6CAE, label=_$!<Home>!$_, value=<CNPhoneNumber: 0x7b72ecd0: countryCode=us, digits=8885551212>>"
If the phone number is localized with phone characters like 1 (234) 456-78, stringValue returns exactly that string.
To obtain the digits, the correct code (in Swift) is
(contact.phoneNumbers[0].value as! CNPhoneNumber).valueForKey("digits") as! String
The digits are a private variable.
For getting a phone number:
CNLabeledValue<CNPhoneNumber*>* labeledValue = contact.phoneNumbers[0];
NSString *phone = labeledValue.value.stringValue;
E.g. to get a mobile number:
// get mobile
NSString *phone = nil;
for (CNLabeledValue<CNPhoneNumber*>* labeledValue in contact.phoneNumbers)
{
if ([labeledValue.label isEqualToString:CNLabelPhoneNumberMobile])
{
phoneNumber = labeledValue.value.stringValue;
break;
}
}
This worked for me in Swift 2.1. / Xcode 7.2. and iOS 9.2.1.
import ContactsUI
Import the contacts framework at the bottom of your file
Get your contacts from your addressBook
This is my example:
let contacts = XXXContactsAccessManagerM.sharedContacts.getAll()
Add this code to the class Model or ViewController in which you are working
for var ctcCtr = 0 ; ctcCtr < contacts.count ; ctcCtr++
{
let contact = contacts[ctcCtr]
let phoneNumbers = contact.phoneNumbers
for var phnCtr = 0 ; phnCtr < phoneNumbers.count ; phnCtr++
{
let phoneNumberLabel = phoneNumbers[phnCtr]
let phoneNumber = (phoneNumberLabel.value as! CNPhoneNumber).valueForKey("digits") as! String
print(phoneNumber)
}
}
I got a normal phone number, the exact way I wrote them in my phone book 097XXXXXXX and so on... (with out hte (XXX) and XXX-XXX)
Using KPKContacts package was simple as calling
contact.firstNumberAvailable() which returns a String value
or you can do contact.numbers.first!.number
This code very helpful for getting both Phone number value and phone number label in swift 3.1
let ContactNumber: NSString = (phoneNumber.value).value(forKey:"digits") as! NSString
print(ContactNumber)
let localizedLabel = CNLabeledValue<NSString>.localizedString(forLabel: phoneNumber.label!)
print(localizedLabel)

Parsing JSON Dictionary elements into an NSArray

I am trying to parse a JSON response into an NSArray for a specific item (geometry/location/lat). I easily manage to get the name into an array with
for (NSDictionary *object in results)
{
NSString *objectName = object[#"name"];
[MyArray addObject:objectName];
}
but I don't know how to dig down to the "lat" level in order to extract this...
JSON DATA full results: (
{
geometry = {
location = {
lat = “15.122494";
lng = “45.518496";
};
};
icon = "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business.png";
id = 1767ee03046d7ac7273be472310cfb6d7b2e6;
name = “Cafe Syracuse”;
photos = (
{
height = 768;
"html_attributions" = (
"From a Google User"
);
"photo_reference" = "CnRoAAAAlVZmfP0Dn3HIwcaEozcjYHqPMC_VlWcvz2sH0yYNjv9DS1GIccAvAp-LNaWxAWKvnEifhPU-1b8ETx7sXtOR7UKSs7tOBpai4FPM1y6M-lcL2qJ4M0dizbHStidxuGCF_QnE548qsE1tP2ZTdhjY_BIQKxSTS_-rPxGlOZmAG7oUUBSiV276H4CemarEXRozEtVr5Vs";
width = 1024;
}
);
"place_id" = ChIJB6K6CExRr8f4AvaA09U;
rating = "4.5";
reference = "CnRjAAAA5Yh4QrEnn7xH2URR7-gwwrhbUz2qmlwbCRW4-JkMfINdldXIYhQR8p2kTdiZiYsdX67nwINp963XIxysFPWOAVZ-okFCu0VH9NL66EWiPSVBFGSgYKXCd7LuhOFMgeU1yLuegyCMQ7RX6mjkhIQiVVok262SeOJGsChxoUXx6DKhp7t_uM51w2y9_bJAlscAA";
scope = GOOGLE;
types = (
cafe,
establishment
);
vicinity = “White glacier";
},
{
geometry = //Other item
Many thanks
This would help you to get lat and long:
NSString*lat= [[[object valueForKey:#"geometry"] valueForKey:#"location"] valueForKey:#"lat"];
NSString *lng= [[[object valueForKey:#"geometry"] valueForKey:#"location"] valueForKey:#"lng"];

get var from an array

Using that code I'm able to get all the list of array.
Looking a lot of tutorial but I'm not able to get the var called "id" if the "name" isEqual to "Timeline Photos".
Code:
NSArray *feed =[result objectForKey:#"data"];
for (NSDictionary *dict in feed) {
NSLog(#"%#",dict);
}
Result (Example of the first two result):
2015-02-03 13:59:21.246 Project[708:29363] {
"can_upload" = 0;
count = 68;
"cover_photo" = 10200808394440000;
"created_time" = "2011-03-09T17:25:03+0000";
from = {
id = 10204248513160000;
name = "XX XX";
};
id = 1716972770000;
link = "https://www.facebook.com/album.php?..";
name = "Mobile Uploads";
privacy = everyone;
type = mobile;
"updated_time" = "2015-01-31T13:28:32+0000";
}
2015-02-03 13:59:21.247 Project[708:29363] {
"can_upload" = 0;
count = 11;
"cover_photo" = 4383404270000;
"created_time" = "2010-02-15T15:50:30+0000";
from = {
id = 10204248513160000;
name = "XX XX";
};
id = 1267183610000;
link = "https://www.facebook.com/album.php?...";
name = "Timeline Photos";
privacy = everyone;
type = wall;
"updated_time" = "2015-01-28T18:26:52+0000";
}
Try this:
NSArray *feed =[result objectForKey:#"data"];
for (NSDictionary *dict in feed) {
if ([dict[#"name"] isEqualToString:#"Timeline Photos"])
NSLog(#"%#",dict[#"id"]);
}
This will only print id when name is equal to Timeline Photos.
Actually, you are not working with an array, but with a dictionary.
Hope this helps.

How to extract elements in NSArray using NSPredicate

I have an array (NSArray) with the following content:
dates {
dateOne = {
"date_one" = "2011-11-30";
name = "test1";
dateOne=true
};
dateTwo = {
"date_two" = "2011-11-30";
name = "test2";
dateTwo=false
};
dateThree = {
"date_Three" = "2011-11-30";
dateTwo=true;
name = "test3";
};
the content above is being generating with the following line of code:
NSArray *dates = [myXmlParsed valueForKeyPath:#"myXml.dates"];
using NSLog:
NSLog(#"%#, '%#'", NSStringFromClass([dates class]), dates);
I get the following output:
__NSDictionaryM, '{
dateOne = {
"date_one" = "2011-11-30";
name = "test1";
dateOne=true
};
dateTwo = {
"date_two" = "2011-11-30";
name = "test2";
dateTwo=false
name = "test3";
};
dateThree = {
"date_Three" = "2011-11-30";
name = "test1";
dateTwo=true;
name = "test3";
};
}'
but I'm having problems getting the values of dictionaries inside of the array. My question is how can get the data of dateThree and how can I access to individual node of information for example date_Three ?
I'll really appreciate your help guys
I figure it out. I was using nsarray when I should have done nsmutabledictionary.
NSMutableDictionary *dates=[[NSMutableDictionary alloc] initWithDictionary: [resultDict valueForKeyPath:#"myXml.dates"]];
Now I can query any of the values:
NSLog(#"value %#", [[dates valueForKey:#"dateOne"] valueForKey:#"date_one"]);

Resources