Swift assign a float variable [duplicate] - ios

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
I found a issue that seems strange, or something I don't understand. The following is a very simple variable assignment:
let f: Float = 310.15
In debugger, I see it is 310.149994
I need to send the value as 310.15 to server, but 310.149994 is received instead.
Is this a Swift compiler or runtime bug? Or any way I can ensure 310.15 float is sent? (I cannot change the server API signature)

Try this one
let f : Float = 310.149954
print(round(1000*f)/1000)
result : 310.15

Related

Swift 4: 'substring(to:)' is deprecated [duplicate]

This question already has answers here:
How can I use String substring in Swift 4? 'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator
(21 answers)
Closed 5 years ago.
I'm having trouble converting my Swift 3 code to Swift 4. I've managed to translate everything else in the app successfully, but am having trouble with a single line of code:
cleanURL = cleanURL.substring(to: cleanURL.index(before: cleanURL.endIndex))
The error I'm getting is this:
ViewController.swift:62:33: 'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range upto' operator.
Well, do what the error says, use the String slicing subscript (subscript(_:)) with a 'partial range upto' operator (..<, docs, where it's called a 'half open range', oddly enough):
let actuallyCleanURL = kindaCleanURL[..<kindaCleanURL.endIndex]
Note that this returns a Substring. If you need to do more slicing operations, do them on this substring. Once you're done, promote your to a String by running it through the String initializer (String(mySubString)), causing a copy of the memory to be made.

Swift compiler error: missing argument labels [duplicate]

This question already has answers here:
Swift 3 first parameter names
(5 answers)
Closed 5 years ago.
I run Swift on Xcode 8.3.3. Here I get an error regarding closures.
It seems that this part is all correct [var intro = introToFriends("Jim", "Pam")
intro] yet isn't clicking.
What's the issue?
Later Swift versions require explicit argument labels when calling a function. In your case it needs to read:
var intro = introToFriends(friendOne: "Jim", friendTwo: "Pam")
Alternatively, you can allow for omitting the use of argument labels if you add underscores to the parameter labels in the function declaration such as:
func introToFriends(_ friendOne: String, _ friendTwoString) {
...
}
For more details check the official Apple documentation on function argument labels.

Absolute value function abs() unavailable in Swift 3.0. Use abs(_:) free function? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am converting a Swift iOS project to Swift 3.0 using Xcode 8 Beta 4. The absolute value function shows to be unavailable. Here is a code sample...
let myInt = -3
let myAbsoluteInt = abs(myInt)
The second line results in an error stating: 'abs' is unavailable: Please use the 'abs(_:)' free function.
Ideas?
The code you're showing us works for me in Xcode 8 beta 4.
I suppose you have another, different issue causing this misleading error message.
For example, it could be a conflict if you have imported other libraries.
A quick workaround would be to use the complete type:
let myAbsoluteInt = Swift.abs(myInt)
You can try explicit Int as parameter as mentioned here it may be a bug :
Ambiguous use of operator '-' in Swift with 'abs()'
let myInt:Int = -3
let myAbsoluteInt = abs(myInt)

swift UITest: Missing argument for parameter #1 in call [duplicate]

This question already has answers here:
Swift UITesting error: Invalid escape sequence in literal. \U201c
(2 answers)
Closed 7 years ago.
I am running the UITest for my swift project, after i click a UITextField it generated this code:
let secureTextField = app.secureTextFields["\U5bc6\U7801"]
secureTextField.tap()
The error is
/XXXUITests.swift:46:60: Invalid escape sequence in literal,
then i searched and changed ["\U5bc6\U7801"] to ["\u{5bc6}\u{7801}"], i got the error
Missing argument for parameter #1 in call
Any help?
Just not sure why i re-generated the code the error disappeared.

Who calls the function in objective-c? [duplicate]

This question already has answers here:
Objective-C find caller of method
(12 answers)
Closed 7 years ago.
Is it possible to know which function calls the current function in the objective-c?
for example, I have 100 functions give a call to function X. In the function X, are we able to define which function from 100 functions has already called?
Thanks.
I'm using a define directive :
#define CALLER_OF_METHOD NSLog(#"My Caller: [%#]", [[[[NSThread callStackSymbols] objectAtIndex:1] componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"[]"]] objectAtIndex:1])
please try, let me know if it's works for you chipbk10
if you dont want to write any code, put a break point in the function, then on the left it will show you the calling tree and you can see what functions were called in order to get to that breakpoint
Need to run application set break point in function X. When application stops at break points check functions in Debug navigator in xcode. you will know that which function called function X.See the sequence in below image.

Resources