Objective C using UItextview/NSstring place holders in long strings - ios

please tell me why I get an: Interface type can not be Statically allocated,
error on the code below and what I can do to be able to use this placeholder in the Json string I am building below. Email is a UITextfield.
NSString *CCEmail = email.text;
NSString *jsonInputString =
#"{\"email\": \" %# \",\"password\": \"iamlearningtocode\"}",CCEmail;

To get the %# in your string replaced with the text in CCEmail you need to call
NSString *jsonInputString = [NSString stringWithFormat:#"{\"email\": \" %# \",\"password\": \"iamlearningtocode\"}", CCEmail];
However with this approach a CCEmail containing " would cause the string to become invalid JSON. I suggest you build your data in a proper NSDictionary and use NSJSONSerialization to convert to string.

You have to use stringWithFormat method of NSString class.
NSString *jsonInputString = [NSString stringWithFormat:#"{\"email\": \" %# \",\"password\": \"iamlearningtocode\"}",CCEmail];

Related

stringByReplacingOcurrencesOfString with special character objective c

I am trying to replace instances of 's with s or alternatively instances of s with 's. However, the result of the code below is an empty string. What could I be doing wrong?
NSString *myStr = #"Eat at Joe's";
NSString *newStr = [myStr stringByReplacingOccurrencesOfString:#"\'s" withString:#"s"];
//edited as per Vadian
NSLog(#"newStr:%#",newStr); //logs as newStr:
You forgot the placeholder
NSLog(#"newStr: %#", newStr);
Don't you get the warning
Data argument not used by format string

how to replace the content of NSMutablestring in objective c

I am new in objective c. I want to replace the content of mutable string I am using code as
NSMutableString *myMutableStringObj = [myMutableStringObj stringByReplacingOccurrencesOfString:#" & " withString:#"And"];
But It Shows me warning
Incomparable pointer types assigning to NSMutableString * from NSString *
Any Suggestion about this. I am using MutableString.
It tells you this function returns NSString *. If you want to clean that warning. Just do like this:
NSMutableString *myMutableStringObj = [[myMutableStringObj stringByReplacingOccurrencesOfString:#" & " withString:#"And"] mutableCopy];
See the method signeture from NSString Class.
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target
withString:(NSString *)replacement
this method return NSString object.
So you need to convert result into mutalbe string or catch the result in NSString.
Read the documentation for NSString and NSMutableString. That doesn't only apply to you, but to anyone answering here so far. The answers on this thread have been absolutely depressing.
There are methods that can be used for NSString but also work with NSMutableString which create a new, immutable string based on a previous string. Typically methods starting with "stringBy..." will be creating a new string.
And there are methods, in the NSMutableString interface, which modify the contents of an existing NSMutableString. Like "replaceOccurencesOfString".
Read the documentation and pick what is right for you. A method starting with "stringBy" is not going to modify your mutable string, it's creating a new immutable one. To modify a string, use the methods from the NSMutableString interface. Fortunately the compiler tells you when you use the wrong method.
plz use this code
NSMutableString * str1 = [NSMutableString stringWithString:#"Hello Testing"];
str1 = [[str1 stringByReplacingOccurrencesOfString:#"T" withString:#"B"] mutableCopy];
NSLog(#"%#",str1);

iOS finding string within a string

Hello everyone I am trying find a string inside a string
lets say I have a string:
word1/word2/word3
I want to find the word from the end of the string to the last "/"
so what I will get from that string is:
Word3
How do I do that?
Thanks!
You are looking for the componentsSeparatedByString: method
NSString *originalString = #"word1/word2/word3";
NSArray *separatedArray = [originalString componentsSeparatedByString:#"/"];
NSString *lastObject = [separatedArray lastObject]; //word3
once check this one By using this one you'l get last pathcomponent values,
NSString* theFileName = #"how /are / you ";
NSString *str1=[theFileName lastPathComponent];
NSLog(#"%#",str1);
By using lastPathComponent you'l get the last path component directly no need to take array for separate the string.
you must use NSScanner class to split substring.
check this.
Objective C: How to extract part of a String (e.g. start with '#')
NSString *string = #"word1/word2/word3"
NSArray *arr = [string componentsSeperatedByString:#"/"];
NSSting *str = [arr lastObject];
You can find it also with this way:
NSMutableString *string=[NSMutableString stringWithString:#"word1/word2/word3"];
NSRange range=[string rangeOfString:#"/" options:NSBackwardsSearch];
NSString *subString=[string substringFromIndex:range.location+1];
NSRegularExpression or NSString rangeOfString:options:range:locale: (with options to search backwards).
The answer really depends on exactly what the input string will contain (how consistent it is).

Chinese Character Encoding Issue ios

I have one app in that I have support four Languages. In that When I am login with Chinese User name at that time it shows me Response like this ..
[{"0":"41","intid":"41","1":"\u8a00\u3046","varfirstname":"\u8a00\u3046","2":"\u8a00\u3046","varlastname":"\u8a00\u3046","3":"\u5730","varusername":"\u5730","4":"abc#gmail.com","varemailid":"abc#gmail.com","5":"qwert","varpassword":"qwert","6":"12345","varmobileno":"12345","7":"Enable","mobileMessage":"Enable","8":"","varphoneno":"","9":"Enable","enumstatus":"Enable","10":"2013-01-30","date_insert":"2013-01-30","11":"2013-01-30","date_edit":"2013-01-30","12":"1.38.28.36","varipaddress":"1.38.28.36"}]
I want to Show "varfirstname" to UITextfield Text . But I am not getting any Text when I print it in NSLog .
NSLog(#"Text is === %#",textfname,text);
How can I decode this Text? And show it on UITextfield or UILabel.
I just searched it and found one of the useful Answer from here.
It's natural that Chinese and Japanese characters don't work with ASCII string encoding. If you try to escape the string by Apple's methods, which you definitely should to avoid code duplication, store the result as a Unicode string. Use one of the following encodings:
NSUTF8StringEncoding
NSUTF16StringEncoding
NSShiftJISStringEncoding (not Unicode, Japanese-specific)
UPDATE
For Example you can encode Decode your chinese String like below:
NSString * test = #"汉字马拉松是";
NSString* encodedString =[test stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"====%#",encodedString);
OUTPUT IS:
%E6%B1%89%E5%AD%97%E9%A9%AC%E6%8B%89%E6%9D%BE%E6%98%AF
Then Decode it like:
NSString* originalString =[encodedString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"====%#",originalString);
OUTPUT IS:
汉字马拉松是
NSString *abc = #"\u8a00\u3046";
NSLog(#" %# " , [NSString stringWithUTF8String:[abc UTF8String]]);
and if you use json :
NSString *html = #"\u8a00\u3046";
NSData *jsonData = [html dataUsingEncoding:NSUTF8StringEncoding];
NSLog(#" %# " , [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);
they all output "言う" I think it is Japanese

Encrypted twitter feed

I'm developing an iOS application , that will take a twits from twitter,
I'm using the following API
https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&count=2&screen_name=TareqAlSuwaidan
The problem are feed in Arabic Language ,
i.e the text feed appears like this
\u0623\u0646\u0643 \u0648\u0627\u0647\u0645
How can i get the real text (or how to encode this to get real text) ?
This is not encrypted, it is unicode. The codes 0600 - 06ff is Arabic. NSString handles unicode.
Here is an example:
NSString *string = #"\u0623\u0646\u0643 \u0648\u0627\u0647\u0645";
NSLog(#"string: '%#'", string);
NSLog output:
string: 'أنك واهم'
The only question is exactly what problem are you seeing, are you getting the Arabic text? Are you using NSJSONSerialization to deserialize the JSON? If so there should be no problem.
Here is an example with the question URL (don't use synchronous requests in production code):
NSURL *url = [NSURL URLWithString:#"https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&count=2&screen_name=TareqAlSuwaidan"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error;
NSArray *jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
NSDictionary *object1 = [jsonObject objectAtIndex:0];
NSString *text = [object1 objectForKey:#"text"];
NSLog(#"text: '%#'", text);
NSLog output:
text: '#Naser_Albdya أيدت الثورة السورية منذ بدايتها وارجع لليوتوب واكتب( سوريا السويدان )
Those are Unicode literals. I think all that's needed is to use NSString's stringWithUTF8String: method on the string you have. That should use NSString's native Unicode handling to convert the literals to the actual characters. Example:
NSString *directFromTwitter = [twitterInterface getTweet];
// directFromTwitter contains "\u0623\u0646\u0643 \u0648\u0627\u0647\u0645"
NSString *encodedString = [NSString stringWithUTF8String:[directFromTwitter UTF8String]];
// encodedString contains "أنك واهم", or something like it
The method call inside the conversion call ([directFromTwitter UTF8String]) is to get access to the raw bytes of the string, that are used by stringWithUTF8String. I'm not exactly sure on what those code points come out to, I just relied on Python to do the conversion.

Resources