天道酬勤,学无止境

nsstring

IOS:Convert string to hexadecimal array

Question i have string representing data .i need to convert those data to the hex array .By using the hex array data i can pass it to the CRC for writing to the peripheral My string data is like this NSString *stringsdata=@"helloworld1234567812345q"; i need to convert to hex format array like {0x0h,0x0e............0x0q}. so by using this array i can keep the data in the crc and write it to the peripheral data as Byte comm[24]; comm[0]=0x01; comm[1]=0x30; comm[2]=0x62; comm[3]=0x00;................ have tried with many possible solutions but not luck.can any body help will be greatly

2021-06-21 10:03:39    分类:技术分享    ios   objective-c   nsstring   hex   core-bluetooth

How can i implement the behaviour as in cluster pattern by apple (NSString and NSCFString)

Question I am simply writing the following code for testing purpose: NSString *aStr = [[NSString alloc] initWithFormat:@"Foo"]; aStr = [aStr initWithFormat:@"Bar"];//Crashed here I am getting the following error: *** initialization method -initWithFormat:locale:arguments: cannot be sent to an abstract object of class __NSCFString: Create a concrete instance! If i write the following code same thing happen NSString *aStr = [NSString alloc]; aStr = [aStr initWithFormat:@"Foo"]; aStr = [aStr initWithFormat:@"Bar"]; //Crashed here By google I come to know that initWithFormat will return the

2021-06-12 17:22:14    分类:技术分享    ios   objective-c   nsstring

Converting NSDecimalNumber to NSString

Question I'm retrieving a key from an object that looks like this: po obj { TypeID = 3; TypeName = Asset; } The key value is being retrieved like this: NSString *typeId = (NSString*)[obj objectForKey:@"TypeID"]; Rather than typeId being an NSString, it is an NSDecimalNumber. Why is that? How do I convert it to an NSString? Answer1 You need to use the stringWithFormat function: NSString *typeId = [NSString stringWithFormat:@"%@", [obj objectForKey:@"TypeID"]]; or stringValue: NSString *typeId = [[obj objectForKey:@"TypeID"] stringValue]; Answer2 This should work: NSString *typeId = [[obj

2021-06-12 11:29:38    分类:技术分享    objective-c   ios   cocoa-touch   nsstring   nsdecimalnumber

How can I escape unicode characters in a NSString?

Question When I store a NSString inside some NSDictionary and log that dictionary to the console like this: NSString *someString = @"Münster"; NSDictionary *someDict = [ NSDictionary dictionaryWithObjectsAndKeys: someString, @"thestring" ]; NSLog ( @"someDict: %@", [ someDict description ] ); The console output looks like this: unicode_test[3621:903] someDict: { thestring = "M\U00fcnster"; } with the string's unicode character escaped. Is there any method to convert a NSString to this escaped representation? Answer1 The problem could be solved using a loop on a UniChar-string representation of

2021-06-12 10:41:19    分类:技术分享    cocoa   unicode   nsstring   escaping

Replace occurrences of NSString - iPhone

Question I have a long NSString in which I m trying to replace special characters. Part of my string looks like this: "veau (c\u00f4telette)","veau (filet)","agneau (gigot)","agneau (c\u00f4telette)","b**\u0153**uf (hach\u00e9)","porc (hach\u00e9)" I would like to replace all the \u0153 with "oe". I've tried: [response stringByReplacingOccurrencesOfString:@"\u0153" withString:@"oe"]; but it doesn't work.... I don't understand why! Answer1 The backslash is an escape character, so if you want to specify the actual backslash character in a string literal, you need to use two backslashes. NSString

2021-06-12 07:40:54    分类:技术分享    iphone   objective-c   replace   nsstring

Import csv data (SDK iphone)

Question For the following code, I can read all the data in the string, and successfully get the data for plot. NSMutableArray *contentArray = [NSMutableArray array]; NSString *filePath = @"995,995,995,995,995,995,995,995,1000,997,995,994,992,993,992,989,988,987,990,993,989"; NSArray *myText = [filePath componentsSeparatedByString:@","]; NSInteger idx; for (idx = 0; idx < myText.count; idx++) { NSString *data =[myText objectAtIndex:idx]; NSLog(@"%@", data); id x = [NSNumber numberWithFloat:0+idx*0.002777778]; id y = [NSDecimalNumber decimalNumberWithString:data]; [contentArray addObject:

2021-06-12 05:36:59    分类:技术分享    ios   iphone   cocoa-touch   csv   nsstring

How to capitalize the first word of the sentence in Objective-C?

Question I've already found how to capitalize all words of the sentence, but not the first word only. NSString *txt =@"hi my friends!" [txt capitalizedString]; I don't want to change to lower case and capitalize the first char. I'd like to capitalize the first word only without change the others. Answer1 Here is another go at it: NSString *txt = @"hi my friends!"; txt = [txt stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[txt substringToIndex:1] uppercaseString]]; For Swift language: txt.replaceRange(txt.startIndex...txt.startIndex, with: String(txt[txt.startIndex])

2021-06-12 05:00:31    分类:技术分享    objective-c   string   cocoa   nsstring   capitalization

Calculate number of differences between two NSStrings

Question How can I calculate the number of differences between two NSStrings. Example: NSString 1 = "this is a string" NSString 2 = "Tihs isa string" should return: 4 (one for the capital "T", one for the "i", the "h" and for the missing space) Answer1 What you're looking for is the Levenshtein Distance. An implementation in Objective-C: ------------------------------------------------------------------------ // // NSString-Levenshtein.h // // Created by Rick Bourner on Sat Aug 09 2003. // rick@bourner.com @interface NSString(Levenshtein) // calculate the smallest distance between all words in

2021-06-12 03:37:33    分类:技术分享    iphone   nsstring   comparison

Returning an NSString from an NSError

Question I am using the NSURLRequest class in my iPhone app, and the method that calls it returns an NSString which is great for when the connection goes through ok, but the issue is I need to convert the NSError into an NSString so I can either return it back or run some if() statements on it. Any ideas? :) Answer1 -[NSError localizedDescription]. (Also, every ObjC object inherited from NSObject implements -description which returns an NSString.) Answer2 for folks new to objective c (me), following is example code that makes accepted answer from 'KennyTM' work -> [self showAlertWithTitle:@

2021-06-12 03:35:19    分类:技术分享    objective-c   nsstring   nserror

Position of a character in a NSString or NSMutableString

Question I have searched for hours now and haven't found a solution for my problem. I have a NSString which looks like the following: "spacer": ["value1", "value2"], "spacer": ["value1", "value2"], ... What I want to do is to remove the [ ] characters from the string. It's seems to be impossible with objective-c. Most other programming languages offer functions like strpos or indexOf which allow me to search for a character or string and locate the position of it. But there seems nothing to be like this in objective-c. Does anyone has an idea on how to remove these characters? Additionally

2021-06-12 00:31:37    分类:技术分享    objective-c   nsstring   replace   nsmutablestring