nsstring

adding zeros in objective-c string formats

余生长醉 提交于 2019-12-11 02:06:08
问题 Quick question: I am trying to fill in empty spaces with a specific number of zeroes in an NSString stringWithFormat formatting string. For example, I want: @"The number is %d", 5 // I want this to output 'the number is 05' @"the number is %d", 10 // I want this to output 'the number is 10' I know how to do this in Java, but I cant seem to find the same function in objective-c. Any help would be great. 回答1: If in Java you use System.out.printf() , then it's the same format syntax in Objective

Best way to initialise / clear a string variable cocoa

余生颓废 提交于 2019-12-11 02:02:41
问题 I have a routine that parses text via a loop. At the end of each record I need to clear my string variables but I read that someString = @"" actually just points to a new string & causes a memory leak. What is the best way to handle this? Should I rather use mutable string vars and use setString:@"" between iterations? 回答1: You have to be careful in which case you create the NSString: (factory method) or (alloc init or (using @""). If you use @"", it is a constant string, see here: Constant

Converting NSString to Float - Adds in Decimal Places?

一世执手 提交于 2019-12-11 01:37:23
问题 I am parsing some vertice information from an XML file which reads as follows (partial extract) : 21081.7 23447.6 2781.62 24207.4 18697.3 -2196.96 I save the string as an NSString and then convert to a float value (which I will later feed into OpenGL ES) NSString * xPoint = [finishedParsingArray objectAtIndex:baseIndex]; NSLog(@"xPoiint is %@", xPoint); float x = [xPoint floatValue]; The problem is that float x changes the values as follows : 21081.699219, 23447.599609, 2781.620117, 24207

convert .zip file into NSData

て烟熏妆下的殇ゞ 提交于 2019-12-11 01:32:59
问题 Hey, Is it correct to initialize an NSData with a zip file? I want to convert a zip file into NSData and construct another file with the data (in simple language 'copy it'). I have the code as: NSURL *theFileUrl = [NSURL URLWithString: @"file://localhost/Users/xxx/Desktop/testZippedFile.zip"]; NSData *data = [NSData dataWithContentsOfURL: theFileUrl]; When I, NSLog(@"Data: %@", data) , i do get some output but when I try to initialize an NSString with this data, it doesn't work: NSString *str

How to fix an NSString Expected expression error in a switch statement? [duplicate]

大憨熊 提交于 2019-12-11 01:10:00
问题 This question already has answers here : Why can't variables be declared in a switch statement? (23 answers) Closed 6 years ago . I'm getting an "expected expression" error in a switch statement on the first line of the code below in this NSString: NSString *emailTitle = @"some text"; break; case 4: // mail // Email Subject NSString *emailTitle = @"some text"; // Email Content NSString *messageBody = @"http://www.example.com/"; // To address NSArray *toRecipents = [NSArray arrayWithObject:@""

Variable argument lists in stringWithFormat for query string

浪尽此生 提交于 2019-12-11 00:28:45
问题 What is the smartest way to build a query with NSString , when the parameter count is variable? Let's say I have a web service, which returns a list of countries filtered by a list of parameters. For example: NSString *continent = @"europe"; NSString *language = @"german"; NSString *timeZone = @"UTC+1"; // to be continued Thus my query string would be: [NSString stringWithFormat:@"/getCountries?continent=%@&language=%@&timezone=%@", continent, language, timeZone]; My response would contain

How To Save Data For Bookmarks?

泪湿孤枕 提交于 2019-12-10 23:53:27
问题 I'm creating a very simple bookmarks menu for my app. I just need to save 3 strings for each object. I was thinking of using core data but I don't want this to be connected with my core database for various reasons. Therefore what other easy options do I have? NSUserDefaults or .plist? I just need to save the 3 strings for each object then load them into a table view to be viewed. 回答1: I'd recommend NSUserDefaults - is certainly easier. I tend to only use plist files for static data that I

If this is the right way to use a customised string property in Objective C, why can’t I extract the correct numeric value?

时光毁灭记忆、已成空白 提交于 2019-12-10 22:57:16
问题 I am revising an early project where I use tags to identify 1-of-5, 1-of-16 or 1-of-10 UIButtons . I want to replace the tags with a customised property based on my understanding of this answer. The property called myInfo consists of a string followed by an integer. This may well be a tag by another name but it makes a message source uniquely identifiable in a way that a simple integer tag does not, clearing magic numbers from my code and, hopefully, improving the documentation. The property

Covert NSString to NSDate in a different timezone

☆樱花仙子☆ 提交于 2019-12-10 22:47:37
问题 I am trying to convert a string to date in a separate time zone from the current system time zone. I have set the time zone of the dateformatter to this timezone. But the NSDate I get is always in my current time zone. For instance, my current time zone is PDT. I have set the DateFormatter's time zone to CST. I have a string with the time in CST. But when I use date formatter to convert this string to date, I get the date in PDT, whereas I want it in CST. Can someone please help. Thanks. 回答1:

Get Specific elements from HTML in UIWebView

馋奶兔 提交于 2019-12-10 22:14:05
问题 how can i get specific elements from html page if i have this nsstring with this value <body style="background-color: 3399CC;font-size: 200.5%;font-family:monospace;font-style:oblique;"> <div id="1" align="center" style="padding-top: 10%;" > <h1 style="color: #FFFFFF">fawazapp</h1> <p style="color: #C0C0C0;"> bbb</p> <p style="color: #FFFFFF;">aaaaaaaaa</p> </div> <div id="2" align="center" style="padding-top: 10%;" > <h1 style="color: #FFFFFF">fawazapp</h1> <p style="color: #C0C0C0;"> bbb</p