stringwithformat

How to prepend url with http:// using stringWithFormat in Swift

雨燕双飞 提交于 2020-01-10 20:58:11
问题 I'm building a custom browser using UIWebView. Use case: User enters "www.abc.com" into the address bar. Error below thrown: Error Domain=WebKitErrorDomain Code=102 "Frame load interrupted" UserInfo=0x19860770 {NSErrorFailingURLKey=file://www.abc.com, NSErrorFailingURLStringKey=file://www.abc.com, NSLocalizedDescription=Frame load interrupted} Reason: the URL needs to be prepended with "http://" I would like to use the stringWithFormat method of NSString, but I can't seem to get the syntax

objective-c stringvalue from double

早过忘川 提交于 2020-01-02 02:55:09
问题 I have the following code: double d1 = 12.123456789012345; NSString *test1 = [NSString stringWithFormat:@"%f", d1]; // string is: 12.123457 NSString *test1 = [NSString stringWithFormat:@"%g", d1]; // string is: 12.1235 How do I get a string value that is exactly the same as d1? 回答1: It may help you to take a look at Apple's guide to String Format Specifiers. %f 64-bit floating-point number %g 64-bit floating-point number (double), printed in the style of %e if the exponent is less than –4 or

comma separated thousand NSString stringWithFormat

萝らか妹 提交于 2019-12-30 00:51:07
问题 Is it possible to display 1000.99 as 1,000.99 using [NSString stringWithFormat:@"£%0.2f", 1000.99] Please guide me if I am not on the right track to achieve this? 回答1: See NSNumberFormatter. It can handle all your numeric formatting needs and do so in an automatically localized fashion, when used correctly (since currencies around the world are often written with different punctuation & formatting). In particular, check out the number formatting guide linked to at the top of the class

Why does the NSString class round inconsistently when formatting numbers?

做~自己de王妃 提交于 2019-12-23 01:41:57
问题 The output of the following two lines seem to show that NSString's stringWithFormat statement does not round numbers consistently. It seems to round up correctly from 0.75 to 0.8. But it rounds 0.25 down to 0.2. Here is the code. NSString *sRoundedScore = [NSString stringWithFormat:@"%.1f", fScore]; NSLog(@"\r\n score before rounding: %f, rounded score: %@", fScore, sRoundedScore); When fScore is assigned to be 0.25, the output is: score before rounding: 0.250000, rounded score: 0.2 When

Memory leak : How to stop?

倖福魔咒の 提交于 2019-12-11 07:15:50
问题 I am using NSObject class to get all the data from a database. When I call that function it will show a memory leak where I assign a string using stringWithFormat. I think when we use this method we should not have to release that string so why does it show a memory leak? Thanks in advance. :) - (void) Allnote { [app.NoteArray removeAllObjects]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex

locationManager avoid (null) string in a Label

隐身守侯 提交于 2019-12-08 03:55:05
问题 I have some code that find the user location and return it into a Label. In some case, the subAdministrativeArea is not available or the thoroughfare and I want to modify my string so it doesn't show (null). I tried with some if to check this but there is a problem if the (null) are more than one. Anyone has an idea about this? Here is my current code that needs to be modified to detect if a placemark object is equal to null: - (void)locationManager:(CLLocationManager *)manager

NSString stringwithformat: Padding 2 strings with unknown length

无人久伴 提交于 2019-12-07 05:43:58
问题 I'm having a problem doing something with NSString stringWithFormat... The problem is as follows: First of all, I have 2 strings. Which are money quantities. The thing, is that i want to create 1 string with these two. For ex: strF = [NSString stringWithFormat:@"First: %@ Second: %@", value1, value2]; I want to know if there is possible to make a correct padding between First and Second.. Assuming the first string is not the same length always.. I want to make this: First: 10,000.00 Second:

Correct use of format specifier to show up to three decimals if needed, otherwise zero decimals?

此生再无相见时 提交于 2019-12-07 04:18:40
问题 I've found %g to show only decimals if needed. If the number is whole, no trailing .000 is added, so thats good. But in the case of for example 1.12345 I want it to short the answer to 1.123. And in the case of 1.000 I want to only show 1, as %g already does. I've tried to specify %.3g in the string, but that doesn't work. If anyone has the answer, I'd be grateful! 回答1: I reviewed the abilities of a "format string" via the IEEE Specification and as I understand it your wished behavior is not

A format specifier such as `%15@` works in NSLog but not with NSString stringWithFormat

♀尐吖头ヾ 提交于 2019-12-06 05:24:57
问题 I found something strange when trying to use width specifiers with %@ . They work fine in NSLog but not with NSString stringWithFormat: . Example: NSString *rightAligned = @"foo"; NSString *leftAligned = @"1"; NSLog(@"| %15@ | %-15@ |", rightAligned, leftAligned); And you get the expected output of: | foo | 1 | But replace the NSLog with stringWithFormat: : NSString *test = [NSString stringWithFormat:@"| %15@ | %-15@ |", rightAligned, leftAligned]; And the value of test is incorrectly: | foo

NSString stringwithformat: Padding 2 strings with unknown length

末鹿安然 提交于 2019-12-05 11:43:54
I'm having a problem doing something with NSString stringWithFormat... The problem is as follows: First of all, I have 2 strings. Which are money quantities. The thing, is that i want to create 1 string with these two. For ex: strF = [NSString stringWithFormat:@"First: %@ Second: %@", value1, value2]; I want to know if there is possible to make a correct padding between First and Second.. Assuming the first string is not the same length always.. I want to make this: First: 10,000.00 Second: 788.00 First: 10.00 Second: 788.00 First: 0.00 Second: 788.00 First: 100.00 Second: 788.00 First: 5.00