nsstring

Read a text file line by line in Swift?

时光毁灭记忆、已成空白 提交于 2019-11-26 19:56:51
问题 Just started learning Swift, I have got my code to read from the text file, and the App displays the content of the Entire Text file. How can I display line by line and call upon that line multiple times? TextFile.txt contains the following. Banana Apple pear strawberry blueberry blackcurrent the following is what currently have.. if let path = NSBundle.mainBundle().pathForResource("TextFile", ofType: "txt"){ var data = String(contentsOfFile:path, encoding: NSUTF8StringEncoding, error: nil)

how to check if NSString = a specific string value?

余生长醉 提交于 2019-11-26 19:47:48
问题 Hi I am woundering if you can check to see if a NSString equals a specific value say for instance a name of a person? I am thinking along the lines of if (mystring == @"Johns"){ //do some stuff in here } 回答1: if ([mystring isEqualToString:@"Johns"]){ //do some stuff in here } 回答2: Here is another method you might want to use in some circumstances: NSArray * validNames = @[ @"foo" , @"bar" , @"bob" ]; if ([validNames indexOfObject:myString].location != NSNotFound) { // The myString is one of

Split an NSString to access one particular piece

拜拜、爱过 提交于 2019-11-26 19:38:20
I have a string like this: @"10/04/2011" and I want to save only the "10" in another string. How can I do that? NSArray* foo = [@"10/04/2011" componentsSeparatedByString: @"/"]; NSString* firstBit = [foo objectAtIndex: 0]; Update 7/3/2018: Now that the question has acquired a Swift tag, I should add the Swift way of doing this. It's pretty much as simple: let substrings = "10/04/2011".split(separator: "/") let firstBit = substrings[0] Although note that it gives you an array of Substring . If you need to convert these back to ordinary strings, use map let strings = "10/04/2011".split(separator

Creating NSData from NSString in Swift

牧云@^-^@ 提交于 2019-11-26 19:34:49
I'm trying to ultimately have an NSMutableURLRequest with a valid HTTPBody , but I can't seem to get my string data (coming from a UITextField ) into a usable NSData object. I've seen this method for going the other way: NSString(data data: NSData!, encoding encoding: UInt) But I can't seem to find any documentation for my use case. I'm open to putting the string into some other type if necessary, but none of the initialization options for NSData using Swift seem to be what I'm looking for. In Swift 3 let data = string.data(using: .utf8) In Swift 2 (or if you already have a NSString instance)

NSString containsString crashes

半腔热情 提交于 2019-11-26 19:33:58
问题 I'm trying to filter an array according to one of it's string fields. Both nameLower and filterLower has NSString value inside, and yet i keep getting: __NSCFString containsString:]: unrecognized selector sent to instance 0x7f876b79e160 -(void) filterFriendsArray:(NSString*)filter { [_filteredFriendsArray removeAllObjects]; for (FacebookUser* user in _friendsArray) { NSString* nameLower = [user.user.name lowercaseString]; NSString* filterLower = [filter lowercaseString]; if ([nameLower

Trim spaces from end of a NSString

会有一股神秘感。 提交于 2019-11-26 19:16:08
I need to remove spaces from the end of a string. How can I do that? Example: if string is "Hello " it must become "Hello" Dan Use -stringByTrimmingCharactersInSet: NSString *string = @" this text has spaces before and after "; NSString *trimmedString = [string stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; (This will trim whitespace characters on both ends). Swift 3 let string = " this text has spaces before and after " let trimmedString = string.trimmingCharacters(in: .whitespacesAndNewlines) Matt H. Another solution involves creating mutable string: /

Difference between NSString literals

本小妞迷上赌 提交于 2019-11-26 19:01:42
What is the difference between these two lines? NSString * string = @"My String"; NSString * string = [[[NSString alloc] initWithString:@"MyString"] autorelease] @"My String" is a literal string compiled into the binary. When loaded, it has a place in memory. The first line declares a variable that points to that point in memory. From the string programming guide: The simplest way to create a string object in source code is to use the Objective-C @"..." construct: NSString *temp = @"/tmp/scratch"; Note that, when creating a string constant in this fashion, you should avoid using anything but 7

NSString with \n or line break

流过昼夜 提交于 2019-11-26 18:58:34
问题 Does anyone know how to use line breaks in NSString? I need to do something like this - [NSString stringWithFormat:@"%@,\n%@", mystring1,mystring2]; 回答1: I just ran into the same issue when overloading -description for a subclass of NSObject. In this situation I was able to use carriage return (\r) instead of newline (\n) to create a line break. [NSString stringWithFormat:@"%@\r%@", mystring1,mystring2]; 回答2: If your string is going in a UIView (e.g a UILabel), you also need to set the number

How to get the size of a NSString

五迷三道 提交于 2019-11-26 18:53:05
A "quicky": how can I get the size (width) of a NSString? I'm trying to see if the string width of a string to see if it is bigger than a given width of screen, case in which I have to "crop" it and append it with "...", getting the usual behavior of a UILabel. string.length won't do the trick since AAAAAAAA and iiiiii have the same length but different sizes (for example). I'm kind of stuck. Thanks a lot. nevan king This is a different approach. Find out the minimum size of the text so that it won't wrap to more than one line. If it wraps to over one line, you can find out using the height.

enum Values to NSString (iOS)

房东的猫 提交于 2019-11-26 18:50:22
问题 I have an enum holding several values: enum {value1, value2, value3} myValue; In a certain point in my app, I wish to check which value of the enum is now active. I'm using NSLog but I'm not clear on how to display the current value of the enum (value1/valu2/valu3/etc...) as a NSString for the NSLog. Anyone? 回答1: This is answered here: a few suggestions on implementation The bottom line is Objective-C is using a regular, old C enum , which is just a glorified set of integers. Given an enum