nsstring

Downloading HTML string into an NSString or Swift String variable in iOS

。_饼干妹妹 提交于 2019-11-27 17:51:40
问题 Alternative titles How to download a web page data in Objective-C on iPhone? How to read entire content of an html resource file into an NSString * How to download a web page data in Swift? Saving HTML pages in an NSString How do you get web page data and put the data into a string? Something like: NSString *webPageBody = GetWebPageData("www.mysite.com/webpage.html"); in the webPageBody , I would like to see "html my web page html". 回答1: Swift 3.0 guard let url = URL(string: "http://www

Cocoa - Trim all leading whitespace from NSString

隐身守侯 提交于 2019-11-27 17:49:37
(have searched, but not been able to find a simple solution to this one either here, or in Cocoa docs) Q. How can I trim all leading whitespace only from an NSString? (i.e. leaving any other whitespace intact.) Unfortunately, for my purposes, NSString's stringByTrimmingCharactersInSet method works on both leading and trailing. Mac OS X 10.4 compatibility needed, manual GC. John Franklin This creates an NSString category to do what you need. With this, you can call NSString *newString = [mystring stringByTrimmingLeadingWhitespace]; to get a copy minus leading whitespace. (Code is untested, may

NSString with \\n or line break

前提是你 提交于 2019-11-27 17:25:22
Does anyone know how to use line breaks in NSString? I need to do something like this - [NSString stringWithFormat:@"%@,\n%@", mystring1,mystring2]; 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]; If your string is going in a UIView (e.g a UILabel), you also need to set the number of lines to 0 myView.numberOfLines=0; I found that when I was reading strings in from a .plist file, occurrences

Using NSPredicate to determine if a string equals another string

岁酱吖の 提交于 2019-11-27 17:07:22
问题 I have an NSArray of CalEvents returned with the [CalCalendarStore eventPredicateWithStartDate] method. From the events returned, I am trying to keep only those in which the title of the event == @"on call" (case-insensitive). I am able to keep in the array those events whose title includes @"on call" with the following code (where 'events' is a 'NSArray' populated with CalEvents ): NSPredicate *onCallPredicate = [NSPredicate predicateWithFormat:@"(SELF.title CONTAINS[c] 'on call')"]; [events

NSString stringWithFormat adding a percent [duplicate]

爷,独闯天下 提交于 2019-11-27 17:06:37
问题 This question already has an answer here: How to add percent sign to NSString 7 answers How can I add a percent to my stringWithFormat function? For example, I'd like to have the following: float someFloat = 40.233f; NSString *str = [NSString stringWithFormat:@"%.02f%",someFloat]; This should cause the string to be: 40.23% But it is not the case. How can I achieve this? 回答1: % being the character beginning printf-style formats, it simply needs to be doubled: float someFloat = 40.233f;

enum Values to NSString (iOS)

大兔子大兔子 提交于 2019-11-27 17:04:43
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? badMonkey 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 like this: typedef enum { a, b, c } FirstThreeAlpha; Your method would look like this: - (NSString*)

Remove all whitespaces from NSString

跟風遠走 提交于 2019-11-27 16:54:53
I've been trying to get rid of the white spaces in an NSString , but none of the methods I've tried worked. I have "this is a test" and I want to get "thisisatest" . I've used whitespaceCharacterSet , which is supposed to eliminate the white spaces. NSString *search = [searchbar.text stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]]; but I kept getting the same string with spaces. Any ideas? AliSoftware stringByTrimmingCharactersInSet only removes characters from the beginning and the end of the string, not the ones in the middle. 1) If you need to remove only a given

How to evaluate the string equation in ios

喜夏-厌秋 提交于 2019-11-27 15:51:09
问题 Is there way to solve the string equations in ios ? For example Input: NSString * str =@"1+2"; Output: NSInteger result = 3 // i.e sum of 1+2 from str How to go about this and get expected result! Please help! 回答1: You can use NSExpression for this: NSExpression *expression = [NSExpression expressionWithFormat:@"1+2"]; NSLog(@"%@", [expression expressionValueWithObject:nil context:nil]); For further information read the documentation of the used methods. 回答2: If your mathematical expressions

How to create a hex color string UIColor initializer in Swift? [duplicate]

泄露秘密 提交于 2019-11-27 15:40:22
This question already has an answer here: How can I create a UIColor from a hex string? 46 answers I am using this code for create UIColor from hex value. Its working perfectly. extension UIColor { convenience init(red: Int, green: Int, blue: Int) { assert(red >= 0 && red <= 255, "Invalid red component") assert(green >= 0 && green <= 255, "Invalid green component") assert(blue >= 0 && blue <= 255, "Invalid blue component") self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0) } convenience init(netHex:Int) { self.init(red:(netHex >> 16) &

Removing leading zeroes from a string

送分小仙女□ 提交于 2019-11-27 15:28:20
I have a string 0023525631 but what I want is the string 23525631 -- without the leading zeroes. How do I do this? 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 string you can get string with zeros. Or You can reverse string, and iterate it from reverse, if you get some