nsstring

NSString is empty

大城市里の小女人 提交于 2019-12-02 14:29:46
How do you test if an NSString is empty? or all whitespace or nil? with a single method call? You can try something like this: @implementation NSString (JRAdditions) + (BOOL)isStringEmpty:(NSString *)string { if([string length] == 0) { //string is empty or nil return YES; } if(![[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]) { //string is all whitespace return YES; } return NO; } @end Check out the NSString reference on ADC. This is what I use, an Extension to NSString: + (BOOL)isEmptyString:(NSString *)string; // Returns YES if the string

Detecting if an NSString contains…?

空扰寡人 提交于 2019-12-02 14:20:09
How can I detect if a string contains a certain word? For example, I have a string below which reads: @"Here is my string." I'd like to know if I can detect a word in the string, such as "is" for example. Here's how I would do it: NSString *someString = @"Here is my string"; NSRange isRange = [someString rangeOfString:@"is " options:NSCaseInsensitiveSearch]; if(isRange.location == 0) { //found it... } else { NSRange isSpacedRange = [someString rangeOfString:@" is " options:NSCaseInsensitiveSearch]; if(isSpacedRange.location != NSNotFound) { //found it... } } You can easily add this as a

Converting a NSString into NSDate

浪尽此生 提交于 2019-12-02 14:10:08
问题 I have date string: Monday, October 11, 2010. How can I create a NSDate object out of it and then get different components like day, month, date, year from it. Please note that format/locale of this string may change at runtime. 回答1: another possibility: NSCalendar *calendar = [NSCalendar currentCalendar]; NSInteger units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; NSDateComponents *components = [calendar components:units fromDate:date]; NSInteger year = [components year];

“sending 'const NSString *' to parameter of type 'NSString *' discards qualifiers” warning

青春壹個敷衍的年華 提交于 2019-12-02 13:46:34
I have Constants NSString, that I want to call like: [newString isEqualToString:CONSTANT_STRING]; Any wrong code here? I got this warning: sending 'const NSString *' to parameter of type 'NSString *' discards qualifiers How should these be declared? albertamg You should declare your constant string as follows: NSString * const kSomeConstantString = @""; // constant pointer instead of: const NSString * kSomeConstantString = @""; // pointer to constant // equivalent to NSString const * kSomeConstantString = @""; The former is a constant pointer to an NSString object, while the latter is a

NSString unicode encoding problem

青春壹個敷衍的年華 提交于 2019-12-02 13:19:33
I'm having problems converting the string to something readable . I'm using NSString *substring = [NSString stringWithUTF8String:[symbol.data cStringUsingEncoding:NSUTF8StringEncoding]]; but I can't convert \U7ab6\U51b1 into ' It shows as 窶冱 which is what I don't want, it should show as an '. Can anyone help me? it is shown as a ’ That's character U+2019 RIGHT SINGLE QUOTATION MARK. What has happened is you've had the character sequence ’s submitted to you, in the UTF-8 encoding, which comes out as bytes: ’ s E2 80 99 73 That byte sequence has then, incorrectly, been interpreted as if it were

In ios 3des encryption contain lots of null termination.when i convert NSData to NSString , it will endup to first null termination?

淺唱寂寞╮ 提交于 2019-12-02 13:12:08
问题 i have used this function for 3des encryption. ccStatus = CCCrypt(kCCEncrypt, // CCoperation op kCCAlgorithm3DES, // CCAlgorithm alg kCCOptionPKCS7Padding, // kCCOptionPKCS7Padding, //kCCModeECB, // CCOptions [_keyData bytes], // const void *key kCCKeySize3DES, // 3DES key size length 24 bit vinitVec, //iv, // const void *iv, [dTextIn bytes], // const void *dataIn [dTextIn length], // size_t dataInLength bufferPtr, // void *dataOut bufferPtrSize, // size_t dataOutAvailable &movedBytes); //

gethostbyname xcode issues

大城市里の小女人 提交于 2019-12-02 12:13:52
Hey guys, I'm new at this stuff, but I'm trying to use gethostname() to work using xcode and the ipad. I've tried a couple of things, but they all seem to freeze up. So when I just do: host = gethostbyname("website.com"); that works fine. I'd like to be able to change the website from within the program though. I've tried: const char *server = [Website_NSString UTF8String]; host = gethostbyname(server); const char *server = [Website_NSString cStringUsingEncoding:NSASCIIStringEncoding]; host = gethostbyname(server); const char *server[256]; [Website_NSString getCString:server maxLength:256

convert NSArray of NSString(s) to NSArray of NSMutableString(s)

偶尔善良 提交于 2019-12-02 12:02:30
How to do that without having to "scroll" the entire given array with a "for" loop? Josh Caswell Since nobody seems to agree with my comment that this is a duplicate of Better way to convert NSArray of NSNumbers to array of NSStrings , here's the same answer again: NSArray * arrayOfMutableStrings = [arrayOfStrings valueForKey:@"mutableCopy"]; From the docs : valueForKey: Returns an array containing the results of invoking valueForKey: using key on each of the array's objects. - (id)valueForKey:(NSString *)key Parameters key The key to retrieve. Return Value The value of the retrieved key.

Unrecognized selector error for isEqualToString: when setting text of a label

这一生的挚爱 提交于 2019-12-02 10:49:58
I am trying to set the text of a label from one of the properties of my model object ( searchRecipeDetailsVariable ), but I get an error //Extract number of servings from dictionary and place in model self.searchedRecipeDetailsVariable.numberOfServings = [self.detailedSearchYummlyRecipeResults objectForKey: @"numberOfServings"]; //log number of servings to check that it works NSLog(@"Number of Servings, %@",self.searchedRecipeDetailsVariable.numberOfServings); self.numberOfServingsLabel.text = self.searchedRecipeDetailsVariable.numberOfServings; When I print the value, I can see the number

Paste String Data from UIPastboard

三世轮回 提交于 2019-12-02 10:39:40
I made an IOS app that using OCR to scan barcodes and copies the data to UserDefaults.standard.value(forKey: "barcodeId") as! String from a receipt. I'm trying to auto-populate the data taken from I've managed to pass the data to the element ID, But the data scanned using OCR doesn't populate the correct value. Below information from the console. NOTE: I've obscured the URL, Since It's a private URL. Optional(8.906010283900207e+17) is what I need to pass to the element ID on the webbased form. Thanks! Error Domain=WKErrorDomain Code=4 "A JavaScript exception occurred" UserInfo=