Variable names at the end - ios

I am going through the new Apple Swift language.
Why should I use variable name at the end ?
var largest = 0
for (kind, numbers) in interestingNumbers {
for number in numbers {
if number > largest {
largest = number
}
}
}
largest

Are you asking why the last line is largest? Paste the code into a playground and you'll see it show the value of largest on the right at that line. It's just so you can see what the value is after the loop.

If you are following the learning book (I see the code sample taken from the iBook) without Xcode; you are missing Xcode's Playground.
Download Xcode 6 Beta, open playground, paste above code, see the magic ;-)
Playground is a tool to see what happens in your code as you type.
Also to understand the philosophy behind Xcode Playground take a look at this video:
Bret Victor - Inventing on Principle http://vimeo.com/36579366

Related

what's the use of .count command in repeat while loop?

I recently began to study swift as my first programming language and there's a very simple term that i can't find an answer for it.
take a look at this code:
var a = [Int]()
repeat {
let randomNumbers = Int.random(in: 0...10)
if a.contains(randomNumbers) == false {
a.append(randomNumbers)
}
print(randomNumbers)
} while (a.count < 10)
so this code is supposed to add 10 numbers (no duplicates) from 0...10 into the array until all unique integers are listed. what I don't understand is the role of "while" here.
doesn't the last line mean the number of "generated numbers" must be less than 10? then why every time I run the code I get more than 10 numbers (say 30-40) in the console?
Also according to the code, this code must not generate dupes. then why do I get some numbers printed 2,3 times in the console?
you check if randomNumbers already added to array a, if yes, the loop will be executed again, until a.count < 10.
if you move the print statement inside the if statement, it will be printed exactly 10 times.

Call Directory Extension CallKit does't recognize numbers with more than 9 digits

I'm working with CallKit and developing an app with a Call Directory Extension. I've followed this tutorial and I'm currently test the capability of identify numbers that the user does't have in his contacts and show an ID from my app, but although is working perfectly with numbers of 1 to 9 digits, for example 123456, when I set numbers with 10 or more digits, iOs doesn't recognize the number. After a day and a half of google it, I've have found no information about that. If anyone can help me I'll appreciate it. Thanks in advance.
The method for set the phone numbers for recognize:
private func addAllIdentificationPhoneNumbers(to context: CXCallDirectoryExtensionContext) {
// Retrieve phone numbers to identify and their identification labels from data store. For optimal performance and memory usage when there are many phone numbers,
// consider only loading a subset of numbers at a given time and using autorelease pool(s) to release objects allocated during each batch of numbers which are loaded.
//
// Numbers must be provided in numerically ascending order.
let allPhoneNumbers: [CXCallDirectoryPhoneNumber] = [ 123456789, 1_888_555_5555 ]
let labels = [ "ID test", "Local business" ]
for (phoneNumber, label) in zip(allPhoneNumbers, labels) {
context.addIdentificationEntry(withNextSequentialPhoneNumber: phoneNumber, label: label)
}
}
With this code, when I simulate a call with the number 123456789, iOS shows the tag "ID test" and that's correct, but if I add any digit, for example 0 at the end: 1234567890, iOS does't show anything when I simulate a call. I don't know if I'm missing something.
Well, after a bunch of tests I could made it work. The point was that the phone must contain the full country code and the area code. So for example 00_52_55_4567_8932 877 or +52_55_4567_8932 both will work. But 55_4567_8932 and 4567_8932 will not work. I hope this can help someone else in the future. Thank you all!

Showing Doubles in Labels with AND without the decimal-point

To check the best answer, scroll down to Paulw11's answer.
(I apologize for any english mistakes, it's not my 1st language)
I need to solve this problem in order to continue to develop my app.
Here, I got a screenshot. (I know, it's ugly, I'm setting the constraints.)
The problem is: even when the number is a integer, it still shows as a rational. (ex.: 4 appears as 4.0, 16 as 16.0)
What I want is:
When the number in the textfield is integer, I want it to appear without the decimal-point. ( 4 appear as 4, 16 appear as 16)
When the number in the textfield is rational, I want it to appear with the decimal-point that belong to it. (4.2 appears as 4.2, 2.5 as 2.5)
What I don't want to happen:
Round any number. This will ruin the math. As I said, 4.22 needs to be 4.22 . But 4.0 needs to be only 4 .
I'll be very grateful for any help, thank you.
You can simply use the %g formatting option:
deltaValueS.text = String(format:"∆ é %g",deltaValue)
For 4.0 this will give "∆ é 4"
For 4.123 this will give "∆ é 4.123"
Do you want something like this?
let numberA: Float = 1.234
let numberB: Float = 7.000
func getStringFromNumber(number: Float) -> String {
if number - Float(Int(number)) == 0 {
return String("\(Int(number))")
} else {
return String("\(number)")
}
}
print(getStringFromNumber(numberA)) // console output: 1.234
print(getStringFromNumber(numberB)) // console output: 7

Check similarity between two string expressions in Swift

I have scanned text:
Mils, chiiese, wh_ite ch$col_te
And expression list, example:
- cheese
- bread
- white chocolate
- etc.
I need compare broken expression with expression from my list, ex. "white chocolate" with "wh_ite ch$col_te."
Maybe you recommend some frameworks.
String distance - Levenshtein distance
What you need to do is measure the difference between two string. To do that, you can use the Levenshtein distance.
For your luck, somebody already implemented this algorihtm in Swift HERE.
To make it work in Swift 1.2, you'll just have to autofix some errors that occour, nothing too fancy.
You can then use it like this:
println(levenshtein("wh_ite ch$col_te", bStr: "white chocolate")) // prints 3, because you have to change 3 letters to get from aStr to bStr
println(levenshtein("wh_ite ch$col_te", bStr: "whsdfdsite chosdfsdfcolate")) // prints 13, because you have to change 13 letters to get from aStr to bStr
You then just set the tolerance and you are done!
Dejan Skledar's on the right track -- you want to make use of Levenshtein distance. The implementation he points to needs tweaking to work in Swift 1.2, and it tends to be slow. Here's a Swift 1.2-compatible, faster implementation.
Simply include the Tools class in your project. Once you've done that, you can get a number representing the difference between two strings this way:
Tools.levenshtein("cheese", bStr: "chee_e") // returns 1
Tools.levenshtein("butter", bStr: "b_tt_r") // returns 2
Tools.levenshtein("milk", bStr: "butter") // returns 6
Please find the Swift 4 implementation of Joey deVilla's answer here
You have to call the function like below:
Tools.levenshtein(aStr: "Example", bStr: "Examples")
Use StringMetric and be happy
https://github.com/autozimu/StringMetric.swift
import StringMetric
...
"kitten".distance(between: "sitting") // => 0.746
"君子和而不同".distance(between: "小人同而不和") // => 0.555

IOS: Get two digits of a double (€) NOT to display the value, just return it

I´ve read a lot of posts about this but all of them were to limit the number digits to show them(NSString) .In my case I have:
I compare two double values(wich are the "same"), each of them got from different mathematical operations. For example: (4.800000 and 4.800000)
double result1=4.800000, result2=4.800000
//compare the results:
if(result1==result2){
msg.text=#"well done!!";
}else if(result1>result2){
msg.text=#"continue your work";
}
"I´m working with money (4,80€)"
In the msg label i get "continue your work" message, not the "well done". I don´t even know if the comparison is done in a correct way.
I think that the best idea would be to limit 4.800000 to 4.80 in order to delete small values and get a exact comparison.(how could i do this?)
I DONT WANT to limit the number to two digits just to PRINT the solution, I want to WORK with that number.
You can do something like this:
double a = 2.55000, b = 2.55002;
if(fabs(a-b)<1e-3) {
// code here, a == b
} else {
// code here, a != b
}
use floor(<#double#>) to round down OR just subtract them and floor the result.
For a nice answer to this problem see:
https://stackoverflow.com/a/10335601/474896
Which could be summarized as a simple as:
if (fabs(x-y) < FLT_EPSILON) {/* ... */}
However since you you're working with money values you should check out NSDecimalNumber.
Or as Marcus Zarra puts it:
"If you are dealing with currency at all, then you should be using
NSDecimalNumber.".

Resources