nsstring

How to capture last 4 characters from NSString

别等时光非礼了梦想. 提交于 2019-11-27 11:19:56
问题 I am accepting an NSString of random size from a UITextField and passing it over to a method that I am creating that will capture only the last 4 characters entered in the string . I have looked through NSString Class Reference library and the only real option I have found that looks like it will do what I want it to is - (void)getCharacters:(unichar *)buffer range:(NSRange)aRange I have used this once before but with static parameters 'that do not change', But for this implementation I am

NSString : easy way to remove UTF-8 accents from a string?

末鹿安然 提交于 2019-11-27 11:06:43
I want to change a sentence, for example: Être ou ne pas être. C'était là-bas. Would become: Etre ou ne pas etre. C'etait la-bas. Is there any easy way to do this with NSString? Or do I have to develop this on my own by checking each char? Luke NSString *str = @"Être ou ne pas être. C'était là-bas."; NSData *data = [str dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *newStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; NSLog(@"%@", newStr); ... or try using NSUTF8StringEncoding instead. List of encoding types here: https://developer.apple

Strip Non-Alphanumeric Characters from an NSString

纵饮孤独 提交于 2019-11-27 11:04:41
I'm looking for a quick and easy way to strip non-alphanumeric characters from an NSString . Probably something using an NSCharacterSet , but I'm tired and nothing seems to return a string containing only the alphanumeric characters in a string. We can do this by splitting and then joining. Requires OS X 10.5+ for the componentsSeparatedByCharactersInSet: NSCharacterSet *charactersToRemove = [[NSCharacterSet alphanumericCharacterSet] invertedSet]; NSString *strippedReplacement = [[someString componentsSeparatedByCharactersInSet:charactersToRemove] componentsJoinedByString:@""]; In Swift, the

Objective-C formatting string for boolean?

浪子不回头ぞ 提交于 2019-11-27 11:01:20
问题 What formatter is used for boolean values? EDIT: Example: NSLog(@" ??", BOOL_VAL); , what is ?? ? 回答1: One way to do it is to convert to strings (since there are only two possibilities, it isn't hard): NSLog(@" %s", BOOL_VAL ? "true" : "false"); I don't think there is a format specifier for boolean values. 回答2: I would recommend NSLog(@"%@", boolValue ? @"YES" : @"NO"); because, um, BOOL s are called YES or NO in Objective-C. 回答3: Use the integer formatter %d , which will print either 0 or 1

Adding a line break to a UITextView

試著忘記壹切 提交于 2019-11-27 11:00:43
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? imti If you want to add newline in Xcode5 / Xcode6 storyboard Attribute-Inspector (AI), you do it this way: Drag out a UITextView. Double-click it to

Encrypted NSData to NSString in obj-c?

痴心易碎 提交于 2019-11-27 10:58:21
I have an iPhone app which encrypts an inputted NSString using CCCrypt (AES256) and a plaintext key. The string and key are given to the encryption method which returns an NSData object. Requesting [data description] where 'data' is the encrypted string data gives an NSString like: "<0b368353 a707e7de 3eee5992 ee69827e e3603dc2 b0dbbc0b 861ca87d f39ce72a>" but when I try to convert that to an NSString, I get "(null)". I need to return an NSString to the user, which can be used to decrypt back to the original string using the same plaintext key. If the 'description' property of the NSData

sizeWithFont method is deprecated. boundingRectWithSize returns an unexpected value

∥☆過路亽.° 提交于 2019-11-27 10:35:55
In iOS7, sizeWithFont is deprecated, so I am using boundingRectWithSize (which returns a CGRect value). My code: UIFont *fontText = [UIFont fontWithName:[AppHandlers zHandler].fontName size:16]; // you can use your font. CGSize maximumLabelSize = CGSizeMake(310, 9999); CGRect textRect = [myString boundingRectWithSize:maximumLabelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:fontText} context:nil]; expectedLabelSize = CGSizeMake(textRect.size.width, textRect.size.height); In textRect , I'm getting a size greater than my maximumLabelSize , a different size

Converting NSString to NSDictionary / JSON

放肆的年华 提交于 2019-11-27 10:33:06
I have the following data saved as an NSString : { Key = ID; Value = { Content = 268; Type = Text; }; }, { Key = ContractTemplateId; Value = { Content = 65; Type = Text; }; }, I want to convert this data to an NSDictionary containing the key value pairs. I am trying first to convert the NSString to a JSON objects as follows : NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; However when I try : NSString * test = [json objectForKey:@"ID"]; NSLog(@"TEST IS %@", test); I receive the value as NULL . Can

Search through NSArray for string

这一生的挚爱 提交于 2019-11-27 10:23:23
问题 I would like to search through my NSArray for a certain string. Example: NSArray has the objects: "dog", "cat", "fat dog", "thing", "another thing", "heck here's another thing" I want to search for the word "another" and put the results into one array, and have the other, non results, into another array that can be filtered further. 回答1: Not tested so might have a syntax error, but you'll get the idea. NSArray* inputArray = [NSArray arrayWithObjects:@"dog", @"cat", @"fat dog", @"thing", @

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

谁说我不能喝 提交于 2019-11-27 10:10:59
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 I get my newDeviceToken as an NSString? kulss use this : NSString * deviceTokenString = [[[