nsstring

Why do two NSStrings taken from user input end up with the same address?

蹲街弑〆低调 提交于 2019-12-06 15:04:36
I am curious to know how the OS or compiler manages the memory usage. Take this example of a login screen. If I enter the same strings for both user ID & password: say "anoop" both times, the following two strings have same addresses: NSString *userID = self.userNameField.stringValue; NSString *password = self.passwordField.stringValue; If I enter "anoop" & "Anoop" respectively, the address changes. How does the compiler know that the the password text is same as the user ID, so that instead of allocating a new memory space it uses the same reference? rob mayoff The answer in this case is that

Autocompletion using UITextView should change text in range

六眼飞鱼酱① 提交于 2019-12-06 15:02:55
问题 I followed Ray Wenderlich's tutorial on text autocompletion here: http://www.raywenderlich.com/336/how-to-auto-complete-with-custom-values And it works great, but it only allows searching for a string that is contained in the textView. I need it to search for multiple strings in the same view. For example: Hi @user, how's @steve Should search for both occurrences of the @. Here is the original code: - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range

SBJSON parsing NSString to NSDictionary

六眼飞鱼酱① 提交于 2019-12-06 14:17:48
I'm trying to parse an NSString that contains JSON data into an NSDictionary using SBJson 3.0.4, but when I do it, I get this error: "WebKit discarded an uncaught exception in the webView:shouldInsertText:replacingDOMRange:givenAction: delegate: -[__NSCFString JSONValue]: unrecognized selector sent to instance 0x6ab7a40" As far as I know (which isn't very far), the JSON I'm getting is valid, so I don't know why this is happening. My code compiles fine too… Here it is: NSString *tempURL = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=true"

NSString strip regular \\ escape characters how?

人盡茶涼 提交于 2019-12-06 13:14:48
I need an efficient piece of code that strips escape characters. This is regular escapes not HTML escape characters. Example: "\"", "\\\\", "\", "\\" I want a general algorithm to strip any kind of escape sequences. Could use any utility like regular expression. (NSString*) unescape:(NSString*) string { .... } This is the answer I wrote: -(NSString*) unescape:(NSString*) string { for(int i = 0; i < string.length; i++) { char a = [string characterAtIndex:i]; if([string characterAtIndex:i] == '\\' ) { string = [string stringByReplacingCharactersInRange:NSMakeRange(i,1) withString:@""]; } }

Recognize special characters in NSString

老子叫甜甜 提交于 2019-12-06 12:49:25
I have a UITextView with text: 🅰🅱🍉👌📧🎏❡ its image: ( This is screen shot of my text, I worry somebody can not see my special character ) If I am an end-user , I just see that there are only 7 characters on textView. But [textView.text length] = 13 I want to split this text into an array like this: array = @[ , , ,... ] But I can't detect where is, where is, where is ... :( Could you help me! Characters outside of the "basic multilingual plane" - in other words, characters whose Unicode value is greater than U+FFFF - are stored in NSString as two UTF-16 characters, a so-called surrogate pair .

Remove all duplicate characters from NSString

烂漫一生 提交于 2019-12-06 12:00:29
问题 How to do this using standard methods (without manual iteration through source string)? PS: At final I want to get sorted characters of source string. I tried to use NSCharacterSet , but can't find a method to convert character set to string (without iterating the set). 回答1: There is no built-in method for this, but it's pretty easy to iterate over the characters of the string and build a new string without duplicates: NSString *input = @"addbcddaa"; NSMutableSet *seenCharacters =

Objective C NSString problem

流过昼夜 提交于 2019-12-06 11:56:19
问题 I have a NSString that I need to examine character by character and: examine char perform calculation loop (until string ends) Any thoughts on the best way to do this? Do I need to convert the NSString to a NSArray or C string? 回答1: The simplest way to go about it is using NSString 's characterAtIndex: method: int charIndex; for (charIndex = 0; charIndex < [myString length]; charIndex++) { unichar testChar = [myString characterAtIndex:charIndex]; //... your code here } 回答2: -characterAtIndex:

Dealing with special characters like (ö, Ä, é, ß) in iOS

ε祈祈猫儿з 提交于 2019-12-06 11:52:39
I am fetching data from URL, and this data contains special characters, such as in the name Désirée . I want to display this in my TableView cell, but when I display it, it looks like this: Dösiröe . How do I make it display correctly? Fetch data NSString *jsonString = [NSString stringWithContentsOfURL:[NSURL URLWithString:URL] encoding: NSStringEncodingConversionAllowLossy error:nil]; SBJSON *parser = [[SBJSON alloc] init]; dictShow = [parser objectWithString:jsonString error:nil]; arr=[dictShow copy]; Display Data in TableView cell.textLabel.text = [arr objectAtIndex:indexPath.row]; just

How can I pass an NSString from one View Controller to another?

大憨熊 提交于 2019-12-06 11:51:42
I made a very simple storyboard based project with two View Controllers. I want to simply access a string declared in VC1 from VC2. The second VC should then display the text in a textfield upon the press of a button. I do not want to use delegation , a separate class for global data or global variables and Extern . Instead, I read that it was easy to achieve variable sharing using a reference to one VC in the other. For my code shown below, XCode didn't complain, however my problem is this: The NSLog in the second VC returns null. If anybody can tell me how to amend the code to pass the

Shorten an NSString?

╄→гoц情女王★ 提交于 2019-12-06 11:48:41
I have a very simple question. Is there a built in method to shorten strings? If not can someone provide an example of doing that in ObjC ? For example: ThisIsAVeryLongString should become ThisIsAV... It needs to check if the string is over a certain amount of characters and if it is shorten it. It's pretty straightforward... NSString *originalString = @"SomethingVeryLong"; int newLength = 9; if (originalString.length > newLength) NSString *shortString = [originalString substringToIndex:newLength]; 来源: https://stackoverflow.com/questions/3303101/shorten-an-nsstring