nsstring

Objective c: Check if integer/int/number

[亡魂溺海] 提交于 2019-12-03 10:31:24
In objective c, how can i check if a string/NSNumber is an integer or int You can use the intValue method on NSString: NSString *myString = @"123"; [myString intValue]; // returns (int)123 Here is the Apple documentation for it - it will return 0 if it's not a valid integer: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/intValue Hope that helps! Anshu Chimala If you're trying to determine whether or not an NSString has a numeric value or not, try using NSNumberFormatter . -(BOOL)

Shortcut to generate an NSRange for entire length of NSString?

随声附和 提交于 2019-12-03 10:27:19
问题 Is there a short way to say "entire string" rather than typing out: NSMakeRange(0, myString.length)] It seems silly that the longest part of this kind of code is the least important (because I usually want to search/replace within entire string)… [myString replaceOccurrencesOfString:@"replace_me" withString:replacementString options:NSCaseInsensitiveSearch range:NSMakeRange(0, myString.length)]; 回答1: Function? Category method? - (NSRange)fullRange { return (NSRange){0, [self length]}; }

Objective C - How to concatenate an entire array of strings?

房东的猫 提交于 2019-12-03 10:27:14
问题 I am an Objective C newbie. I want to write a method that takes in an array of strings and returns a concatenated string, with a comma (,) in between each string. So if an array is {a b c d}, I want to return a,b,c,d. What is the easiest way to do that? 回答1: There are many ways to do it, the simplest being [yourArray componentsJoinedByString: @","] 回答2: Use NSArray's componentsJoinedByString: method. NSArray *strings = ...; NSString *combined = [strings componentsJoinedByString:@","]; 来源:

stringByAddingPercentEscapesUsingEncoding deprecated

匿名 (未验证) 提交于 2019-12-03 10:24:21
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: This is deprecated "stringByAddingPercentEscapesUsingEncoding", I want to replace this line with update one. NSString *encodedString = [modalData.imageURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 回答1: Try below line of code: NSString *encodedString = [modalData.imageURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]; stringByAddingPercentEncodingWithAllowedCharacters: Returns a new string made from the receiver by replacing all characters not in the specified set with

AWSWebIdentityCredentialsProvider How to get parameters values?

匿名 (未验证) 提交于 2019-12-03 10:24:21
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Am trying to use the below code to do authentication, But i am unable to get the values needs to be passed to following parameters, providerId, roleArn, roleSessionName. However, webIdentityToken value i will receive it from our local server. AWSWebIdentityCredentialsProvider *provideer = [AWSWebIdentityCredentialsProvider credentialsWithRegionType:AWSRegionUSEast1 providerId:@"" roleArn:@"" roleSessionName:"" webIdentityToken:@""]; Regards, Bhat 回答1: Class.h ------- @interface AWSCustomCredentialProvider : NSObject <AWSCredentialsProvider>

NSString drawInRect set context

匿名 (未验证) 提交于 2019-12-03 10:24:21
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I draw some image in background. Sometimes my app crashes with strange error: function signature specialization < Arg[0] = Owned To Guaranteed> of MyApp. I checked all crashes, and I get all of them on methods that do not use context. One of them draw text. How can I specify context to draw text? UIGraphicsBeginImageContextWithOptions ( size , false , 1.0 ) let ctx = UIGraphicsGetCurrentContext () // some code str . drawInRect ( CGRectMake ( floor ( 10 * scaleFactor ), yOffset , size . width , floor ( 60 * scaleFactor )),

How do I get the text from UITextField into an NSString?

半腔热情 提交于 2019-12-03 10:20:48
I've tried various methods, which all give me warnings. Such as userName = [NSString stringWithFormat:@"%@", [yournamefield stringValue]]; which just gives me a warning of 'UITextField' may not respond to '-stringValue' How do i do this? Jilouc Get the text inside the text field using the text property NSString *name = yourNameField.text; Eric Chai Use the text property of UITextField : NSString *userName = yourNameField.text; How about: userName = [NSString stringWithFormat:@"%@", yournamefield.text]; Two ways. It is a property and any property value can be accessed like this - yournamefield

NSString replace repeated newlines with single newline

五迷三道 提交于 2019-12-03 10:14:38
问题 I have an NSString which can have multiple \n in between the string. I need to replace the multiple occurrence of \n's with a single \n. I tried this code: newString = [string stringByReplacingOccurrencesOfString:@"\n\n" withString:@"\n"]; But this does not give the desired result if a series of "\n\n\n\n\n\n" is encountered in the string. In this case, they will be replaced with "\n\n\n". What should I do so that all more than one "\n"s be replaced with single "\n"? Thanks to all! Update:

NSString stringWithContentsOfFile failing with what seems to be the wrong error code

佐手、 提交于 2019-12-03 10:12:12
I'm trying to load a file into a string. Here is the code I'm using: NSError *error = nil; NSString *fullPath = [[NSBundle mainBundle] pathForResource:filename ofType:@"html"]; NSString *text = [NSString stringWithContentsOfFile:fullPath encoding:NSUTF8StringEncoding error:&error]; When passed in @"about" as the filename, it works absolutely fine, showing the code works. When passed in @"eula" as the filename, it fails with 'Cocoa error 258', which translates to NSFileReadInvalidFileNameError. However, if I swap the contents of the files over but keep the names the same, the other file fails

Sorting NSMutablearray by NSString as a date

匿名 (未验证) 提交于 2019-12-03 10:10:24
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have an array, which is filled out with string objects. Inside each object is the name of the object and the string, seperated by " - ", ex. "Object1 - 26.05.2012 ". I would like to sort my array by the date in the string, and not the name, descending Is this possible? 回答1: As @Vladimir pointed out, it would be much better to separate the name and the string from each other and sort by the date. NSMutableArray * newArray = [[ NSMutableArray alloc ] init ]; NSDateFormatter * formatter = [[ NSDateFormatter alloc ] init ]; [