Handling QR Code in ios - ios

I am working on QR Codes where where i will get some contents with multiple fields (ex. name,address,contact number etc) from QR Code.
When I tried I am getting response.
But in single string format.
So if I want to separate these contents.
How do I can separate the contents in different fields.
response :
VERSION:3.0
N:Patil;Pradumna
FN:Pradumna Patil
ORG:techsanskar
TITLE:iphone
ADR:;;;;;;
TEL;WORK;VOICE:
TEL;CELL:9420256819
TEL;FAX:
EMAIL;WORK;INTERNET:
URL:
BDAY:
END:VCARD
Thank you.

If you have string in this format name,address,contact number
Like these are seperated by ',' You can use
NSArray *items = [theString componentsSeparatedByString:#","];
NSString *name = items[0];
NSString *address = items[1];//etc

Related

Splitting string into substring in iOS

I have a small doubt on splitting a string into substring.
I want to display State and Country name in one label. i have a string in a service coming from json file "FullAddress": "New Windsor,New York,USA". i want only New York,USA to display in a label.
Use this and get your desire String from Array element 1 and 2
NSArray *strings = [FullAddress componentsSeparatedByString:#","];
Please Use this code it will be help you out
NSString *fullAddress=#"New Windsor,New York,USA";
NSRange range=[fullAddress rangeOfString:#","];
if (range.location!=NSNotFound) {
NSString *string=[fullAddress substringFromIndex:range.location+range.length];//this is address you want
NSLog(#"%#",string);
}

if statements and stringWithFormat

I need to know if there is a way to use if statements to display certain nsstrings, depending on whether or not that NSString contains any data.
I have an nsstringcalled visitorInfo.
The string uses data from other strings (i.e. which operating system the user is running) and displays that info. Here is an example of what I'm talking about:
NSString *visitorInfo = [NSString stringWithFormat:#"INFO\n\nVisitor Location\n%#\n\nVisitor Blood Type\n\%#", _visitor.location, _visitor.bloodType];
And it would display like this:
INFO
Location
Miami, FL
Blood Type
O positive
However, I have several pieces of data that only load if the user chooses to do so. i.e their email address.
This section of code below would do what I want, but my visitorInfo string contains tons of different strings, and if I use this code below, then it won't load any of them if the user chooses not to submit his blood type.
if ([self.visitor.bloodType length] > 0) {
NSString *visitorInfo = [NSString stringWithFormat:#"INFO\n\nVisitor Location\n%#\n\nVisitor Blood Type\n\%#", _visitor.location, _visitor.bloodType];
}
So basically if their is data stored in bloodType then i went that code to run, but if there isn't any data I only want it to skip over bloodType, and finish displaying the rest of the data.
Let me know if you have any more questions
Additional details. I'm using an NSString for a specific reason, which is why I'm not using a dictionary.
Just build up the string as needed using NSMutableString:
NSMutableString *visitorInfo = [NSMutableString stringWithFormat:#"INFO\n\nVisitor Location\n%#, _visitor.location];
if ([self.visitor.bloodType length] > 0) {
[visitorInfo appendFormat:#"\n\nVisitor Blood Type\n\%#", _visitor.bloodType];
}
You can check if a string has any data in it by using the following
if([_visitor.location length]<1){
//This means there's no data and is a better way of checking, rather than isEqualToString:#"".
}else{
//there is some date here
}
** EDIT - (just re-reading your question, sorry this answer is dependant on _visitor.location being a string in the first place)*
I hope this helps
Try this -
NSString *str = #"INFO";
if (_visitor.location) {
str = [NSString stringWithFormat:#"\n\nVisitor Location\n%#",_visitor.location];
}
if (_visitor.bloodType) {
str = [NSString stringWithFormat:#"\n\nVisitor Blood Type\n\%#",_visitor.bloodType];
}

Xcode - UTF-8 String Encoding

I have a strange problem encoding my String
For example:
NSString *str = #"\u0e09\u0e31\u0e19\u0e23\u0e31\u0e01\u0e04\u0e38\u0e13";
NSString *utf = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog("utf: %#", utf);
This worked perfectly in log
utf: ฉันรักคุณ
But, when I try using my string that I parsed from JSON with the same string:
//str is string parse from JSON
NSString *str = [spaces stringByReplacingOccurrencesOfString:#"U" withString:#"u"];
NSLog("str: %#, str);
NSString *utf = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog("utf: %#", utf);
This didn't work in log
str: \u0e09\u0e31\u0e19\u0e23\u0e31\u0e01\u0e04\u0e38\u0e13
utf: \u0e09\u0e31\u0e19\u0e23\u0e31\u0e01\u0e04\u0e38\u0e13
I have been finding the answer for hours but still have no clue
Any would be very much appreciated! Thanks!
The string returned by JSON is actually different - it contains escaped backslashes (for each "\" you see when printing out the JSON string, what it actually contains is #"\").
In contrast, your manually created string already consists of "ฉันรักคุณ" from the beginning. You do not insert backslash characters - instead, #"\u0e09" (et. al.) is a single code point.
You could replace this line
NSString *utf = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
with this line
NSString *utf = str;
and your example output would not change. The stringByReplacingPercentEscapesUsingEncoding: refers to a different kind of escaping. See here about percent encoding.
What you need to actually do, is parse the string for string representations of unicode code points. Here is a link to one potential solution: Using Objective C/Cocoa to unescape unicode characters. However, I would advise you to check out the JSON library you are using (if you are using one) - it's likely that they provide some way to handle this for you transparently. E.g. JSONkit does.

Save Special Character/Swedish/German Characters in NSDictionary

I want to save special characters/german/swedish character in NSDictionary and have to post this data to server, but the data saved in the dictionary is converted to some other format as in console output. I am trying to save this string as different typecasts but not getting.
As NSDictionary's data type is generic, and while sending to POST its sent as in the modified format, I want to save this data in NSDictionary as it is, so that it can be sent in proper format to server and readable at server-end
My code is
NSString *playerName = #"Lëÿlã Råd Sölvê"; // dummy player name
NSLog(#"playerName: %#",playerName);
NSDictionary *postParameters = #{#"playerName1": playerName,
#"playerName2": [NSString stringWithString:playerName],
#"playerName3": [NSString stringWithUTF8String:[playerName UTF8String]],
#"playerName4": [NSString stringWithCString:[playerName UTF8String] encoding:NSASCIIStringEncoding],
#"playerName5": [[NSString alloc] initWithString:playerName],
#"playerName6": [NSString stringWithFormat:#"%#",playerName]};
NSLog(#"postParameters: %#",postParameters);
and output is
playerName: Lëÿlã Råd Sölvê
postParameters: {
playerName1 = "L\U00eb\U00ffl\U00e3 R\U00e5d S\U00f6lv\U00ea";
playerName2 = "L\U00eb\U00ffl\U00e3 R\U00e5d S\U00f6lv\U00ea";
playerName3 = "L\U00eb\U00ffl\U00e3 R\U00e5d S\U00f6lv\U00ea";
playerName4 = "L\U00c3\U00ab\U00c3\U00bfl\U00c3\U00a3 R\U00c3\U00a5d S\U00c3\U00b6lv\U00c3\U00aa";
playerName5 = "L\U00eb\U00ffl\U00e3 R\U00e5d S\U00f6lv\U00ea";
playerName6 = "L\U00eb\U00ffl\U00e3 R\U00e5d S\U00f6lv\U00ea";
}
How can I achieve this...
There is nothing wrong with your code.
What you are seeing is an artefact of NSLog and the description method - the former invokes the latter to obtain the textual representation of an object for output. For NSString the string is displayed using Unicode. However for NSDictionary contained strings are displayed using Objective-C Unicode character escape sequences, which have the form '\Uxxxx'.
To assure yourself all is OK you can use:
for (NSString *key in postParameters)
NSLog(#"%# -> %#", key, postParameters[key]);
and everything should display fine (except playerName4 where you mess the string up yourself).

Using NScanner to parse CSV File to Dictionary Array [duplicate]

This question already has answers here:
Create a Custom Formatted Dictionary Array from CSV File of Data
(3 answers)
Closed 9 years ago.
I've created an iPhone app that has a dictionary array of locations (lat,long,point). I created the array by manually entering each value.
myLocationArray = #[
#{
kStation : #"1",
kLatitude : #( 41.656467),
kLongitude : #(-81.277963)
},
#{
kStation : #"2",
kLatitude : #(41.657118),
kLongitude : #(-81.276545)
},
#{
kStation : #"3",
kLatitude : #(41.658493),
kLongitude : #(-81.273542)
},
...
This is good and works but now I want to create this array programmatically by getting the data from a .CSV file. I have a .CSV file (TestCSV.csv) that looks like this.
41.656467,-81.277963,27200
41.657118,-81.276545,27650
41.658493,-81.273542,28631.5
41.660728,-81.268547,30195
41.661830,-81.266065,30991
41.662828,-81.263819,31700
41.663677,-81.261962,32300
41.664578,-81.259909,32950
41.666210,-81.256312,34100
41.666921,-81.254708,34605
41.668043,-81.252191,35400
41.669044,-81.250043,36099
I'd like to create myLocationArray (with formatting as shown) by parsing TestCSV.csv using NScanner. I've set up to parse the my data file.
NSString *pathToFile =[[NSBundle mainBundle] pathForResource:#"TestCSV" ofType: #"csv"];
NSString *fileString = [NSString stringWithContentsOfFile:pathToFile encoding:NSUTF8StringEncoding error:nil];
if (!fileString) {
NSLog(#"Error reading file.");
}
NSScanner *scanner = [NSScanner scannerWithString:fileString];
I need help from here though. I've looked at many examples but it seems like this is where I need some code custom to my application. Thanks in advance for your time.
I don't understand why you're bothering with a scanner at all, seeing as your structure is so simple and predictable. Start with an empty NSMutableArray. You can split the file into lines like this:
NSArray *lines = [input componentsSeparatedByString:#"\n"];
Now enumerate that array. For each line, you can split the line at the commas:
NSArray *nums = [oneLine componentsSeparatedByString:#","];
The array nums is an array of three NSString values. For the first two items of the array, convert them to doubles with doubleValue and wrap that in an NSNumber. Make the NSDictionary for that line and add it to the NSMutableArray.
Don't try to re-invent the wheel, as there are a couple of pitfalls in the CSV file format (which is not even especially well defined).
There are for instance the special cases of newlines and commas within a field. Any algorithm based on just splitting by lines and commas is going produce incorrect results given such input. Also notice that Excel doesn't always use a comma as a separator, depending on the locale.
Use a library like CHCSVParser, which will cover those pitfalls.

Resources