nsstring

NSString (hex) to bytes

依然范特西╮ 提交于 2019-11-26 17:35:12
Is there any method in Objective-C that converts a hex string to bytes? For example @"1156FFCD3430AA22" to an unsigned char array {0x11, 0x56, 0xFF, ...} . Fastest NSString category implementation that I could think of (cocktail of some examples): - (NSData *)dataFromHexString { const char *chars = [self UTF8String]; int i = 0, len = self.length; NSMutableData *data = [NSMutableData dataWithCapacity:len / 2]; char byteChars[3] = {'\0','\0','\0'}; unsigned long wholeByte; while (i < len) { byteChars[0] = chars[i++]; byteChars[1] = chars[i++]; wholeByte = strtoul(byteChars, NULL, 16); [data

String comparison in Objective-C

北城余情 提交于 2019-11-26 17:34:29
I've currently got a webserver set up which I communicate over SOAP with my iPhone app. I am returning a string containing a GUID and when I attempt to compare this with another string I get some strange results. Why would this not fire? Surely the two strings are a match? NSString *myString = @"hello world"; if(myString == @"hello world") return; Use the -isEqualToString: method to compare the value of two strings. Using the C == operator will simply compare the addresses of the objects. if ([category isEqualToString:@"Some String"]) { // Do stuff... } mxg You can use case-sensitive or case

Removing leading zeroes from a string

假如想象 提交于 2019-11-26 17:10:35
问题 I have a string 0023525631 but what I want is the string 23525631 -- without the leading zeroes. How do I do this? 回答1: You can convert string to integer and integer to string if you are number. Or Declarate pointer to this string and set this pointer to a start of your string, then iterate your string and add to this pointer number of zeros on the start of string. Then you have pointer to string who starting before zeros, you must use pointer to obitain string without zeros, if you use

Combine Two String in different Language RTL & LTR

て烟熏妆下的殇ゞ 提交于 2019-11-26 16:51:01
问题 I have two text, one in Hebrew language and one in English. In first text I have date that is in Hebrew. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; NSLocale *hebrew = [[NSLocale alloc] initWithLocaleIdentifier:@"he_IL"]; // Hebrew [dateFormatter setDateFormat:@"yyyy-MM-dd'T'hh:mm:ss.SSSZ"]; NSDate *date = [dateFormatter dateFromString:model.startDate]; NSLog(@"%@", date); [dateFormatter setDateFormat:@"EEEE,dd.MM.yyyy"]; dateFormatter.locale = hebrew; NSString *strDate =

How do I test if a string is empty in Objective-C?

半城伤御伤魂 提交于 2019-11-26 16:47:07
How do I test if an NSString is empty in Objective-C? Marc Charbonneau You can check if [string length] == 0 . This will check if it's a valid but empty string (@"") as well as if it's nil, since calling length on nil will also return 0. Matt G Marc's answer is correct. But I'll take this opportunity to include a pointer to Wil Shipley's generalized isEmpty , which he shared on his blog : static inline BOOL IsEmpty(id thing) { return thing == nil || ([thing respondsToSelector:@selector(length)] && [(NSData *)thing length] == 0) || ([thing respondsToSelector:@selector(count)] && [(NSArray *

NSString instance reports its class as NSCFString

一世执手 提交于 2019-11-26 16:41:39
问题 My objective here is really simple -- I'm trying to set an NSString to some test data, then return the class, which should be NSString . Here's my code: NSString* stringer = [NSString stringWithFormat: @"Test"]; NSLog(@"%@", [stringer class]); The log says that the class is NSCFString , not NSString . What's going on here? 回答1: NSString is really a container class for different types of string objects. Generally an NSString constructor does return an object that is actually of type NSCFString

What is difference between URLWithString and fileURLWithPath of NSURL?

一个人想着一个人 提交于 2019-11-26 16:26:11
问题 In my code I have to use URLWithString to play streaming( HLS ) video and fileURLWithPath to play local video. What is the difference between these two methods? How should I use single method to play both videos. Also I need to show last frame as still image when HSL video ends. Its now showing blank screen when it ends. How should i achieve this? 回答1: +URLWithString: produces an NSURL that represents the string as given. So the string might be @"http://www.google.com" and the URL represents

Reverse NSString text

烂漫一生 提交于 2019-11-26 16:13:31
I have been googling so much on how to do this, but how would I reverse a NSString? Ex:hi would become: ih I am looking for the easiest way to do this. Thanks! @Vince I made this method: - (IBAction)doneKeyboard { // first retrieve the text of textField1 NSString *myString = field1.text; NSMutableString *reversedString = [NSMutableString string]; NSUInteger charIndex = 0; while(myString && charIndex < [myString length]) { NSRange subStrRange = NSMakeRange(charIndex, 1); [reversedString appendString:[myString substringWithRange:subStrRange]]; charIndex++; } // reversedString is reversed, or

NSTask NSPipe - objective c command line help

瘦欲@ 提交于 2019-11-26 16:09:35
问题 Here is my code: task = [[NSTask alloc] init]; [task setCurrentDirectoryPath:@"/applications/jarvis/brain/"]; [task setLaunchPath:@"/applications/jarvis/brain/server.sh"]; NSPipe * out = [NSPipe pipe]; [task setStandardOutput:out]; [task launch]; [task waitUntilExit]; [task release]; NSFileHandle * read = [out fileHandleForReading]; NSData * dataRead = [read readDataToEndOfFile]; NSString * stringRead = [[[NSString alloc] initWithData:dataRead encoding:NSUTF8StringEncoding] autorelease]; So I

Relative string from NSDate

一世执手 提交于 2019-11-26 16:05:17
问题 Does anyone know of a library or something that will convert an NSDate into strings like the examples below? 1 hour ago yesterday last Thursday 2 days ago last month 6 months ago last year 回答1: NSDateFormatter will do a great deal of what is mentioned above by using setDoesRelativeDateFormatting: on an instance. From there, you can control the formatting using the date style and the time style to fine tune it to your needs. If that doesn't get you what you need then check out