nsstring

Remove all whitespaces from NSString

萝らか妹 提交于 2019-11-26 18:47:59
问题 I've been trying to get rid of the white spaces in an NSString , but none of the methods I've tried worked. I have "this is a test" and I want to get "thisisatest" . I've used whitespaceCharacterSet , which is supposed to eliminate the white spaces. NSString *search = [searchbar.text stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]]; but I kept getting the same string with spaces. Any ideas? 回答1: stringByTrimmingCharactersInSet only removes characters from the

Error getting valueForKey in an NSDictionary for key containing @ character

孤街醉人 提交于 2019-11-26 18:33:09
问题 I am trying to get the value of "@type" key in the following NSDictionary named 'robotDesign' robotDesign : { "@id" = "2"; "@type" = "piggyDash"; turnAngle = "-90"; width = "90.0"; } that is a part of a JSON object I got using [NSJSONSerialization JSONObjectWithData: options: error:]; I am using following code to extract @type value NSString * robot_type = (NSString*)[robotDesign valueForKey:@"@type"]; but recive following error: Terminating app due to uncaught exception

How to create a hex color string UIColor initializer in Swift? [duplicate]

风格不统一 提交于 2019-11-26 18:29:19
问题 This question already has answers here : How can I create a UIColor from a hex string? (45 answers) Closed 4 years ago . I am using this code for create UIColor from hex value. Its working perfectly. extension UIColor { convenience init(red: Int, green: Int, blue: Int) { assert(red >= 0 && red <= 255, "Invalid red component") assert(green >= 0 && green <= 255, "Invalid green component") assert(blue >= 0 && blue <= 255, "Invalid blue component") self.init(red: CGFloat(red) / 255.0, green:

NSString - Convert to pure alphabet only (i.e. remove accents+punctuation)

一曲冷凌霜 提交于 2019-11-26 18:03:37
问题 I'm trying to compare names without any punctuation, spaces, accents etc. At the moment I am doing the following: -(NSString*) prepareString:(NSString*)a { //remove any accents and punctuation; a=[[[NSString alloc] initWithData:[a dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES] encoding:NSASCIIStringEncoding] autorelease]; a=[a stringByReplacingOccurrencesOfString:@" " withString:@""]; a=[a stringByReplacingOccurrencesOfString:@"'" withString:@""]; a=[a

URLWithString: returns nil

浪尽此生 提交于 2019-11-26 18:02:15
问题 it may be very easy, but I don't seems to find out why is URLWithString: returning nil here. //localisationName is a arbitrary string here NSString* webName = [localisationName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSString* stringURL = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@,Montréal,Communauté-Urbaine-de-Montréal,Québec,Canadae&output=csv&oe=utf8&sensor=false&key=", webName]; NSURL* url = [NSURL URLWithString:stringURL]; 回答1: You need to

Is there a simple way to split a NSString into an array of characters?

孤街浪徒 提交于 2019-11-26 18:01:05
Is there a simple way to split a NSString into an array of characters? It would actually be best if the resulting type were a collection of NSString's themselves, just one character each. Yes, I know I can do this in a loop, but I'm wondering if there is a faster way to do this with any existing methods or functions the way you can with LINQ in C#. e.g. // I have this... NSString * fooString = @"Hello"; // And want this... NSArray * fooChars; // <-- Contains the NSStrings, @"H", @"e", @"l", @"l" and @"o" You could do something like this (if you want to use enumerators) NSString *fooString = @

Adding a line break to a UITextView

喜欢而已 提交于 2019-11-26 17:58:47
问题 I have a UITextView that takes an NSString with formatting stringWithUTF8String . It is getting its values from a database and I want the text in the database to be rendered with breaks within the text. I tried using \n to do this but it gets rendered as text. Doing this in my information page of the app as straight text worked but I think the reason its not working when taking it from the database is because of the formatting. Any suggestions? 回答1: If you want to add newline in Xcode5 /

How can I convert my device token (NSData) into an NSString?

倖福魔咒の 提交于 2019-11-26 17:54:49
问题 I am implementing push notifications. I'd like to save my APNS Token as a String. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken { NSString *tokenString = [NSString stringWithUTF8String:[newDeviceToken bytes]]; //[[NSString alloc]initWithData:newDeviceToken encoding:NSUTF8StringEncoding]; NSLog(@"%@", tokenString); NSLog(@"%@", newDeviceToken); } The first line of code prints null. the second prints the token. How can

How do I get word wrap information with the new iOS 7 APIs?

孤者浪人 提交于 2019-11-26 17:47:39
问题 I noticed that iOS 7 introduces new classes related to text layout such as NSLayoutManager, NSTextStorage, and NSTextContainer. How can I use these in order to get information about word wrapping on an NSString? For example, say I have a long NSString which I put in a UILabel. If I enable multiple lines on the UILabel, it would produce a string such as the following: The quick brown fox jumps over the lazy dog. That's great, but I can't access the line breaks in code (e.g. after the word

Weak NSString variable is not nil after setting the only strong reference to nil

不打扰是莪最后的温柔 提交于 2019-11-26 17:45:59
I have a problem with this code : __strong NSString *yourString = @"Your String"; __weak NSString *myString = yourString; yourString = nil; __unsafe_unretained NSString *theirString = myString; NSLog(@"%p %@", yourString, yourString); NSLog(@"%p %@", myString, myString); NSLog(@"%p %@", theirString, theirString); I'm expecting all pointers to be nil at this time, but they are not and I don't understand why. The first (strong) pointer is nil but the other two are not. Why is that? David Rönnqvist tl; dr: The problem is that the string literal never gets released so your weak pointer still