I'm new to Swift and Xcode. I couldn't figure out how to take user input in Swift playground. I'm trying to write Swift equivalent of the following Python code:
def greet():
name = raw_input('What is your name?>')
print "Hello ", name
Is it possible to get user input in Swift playground?
Just use the ask() method which is available on Pad playgrounds
Related
I'm trying to learn a new coding language called swift. But I got a question in mind.
import UIkit
func makeACake (cash:Double, ratio:Double){
print(Making \(cash*ratio)pounds of cake")
}
makeACake(cash:10,ratio:3)
// it will print making 30.0 pounds of cake.
just two questions here.
no.1
why cant i just call makeACake(10,3) why do i have to type it in this manner makeACake(cash:10,ratio:3)
no.2
is the function println removed from swift? why cant i use the function println and it asked me to use print instead.
sorry if i happened to ask any stupid question. but yea just trying to learn coding so maybe help a newbie out? thank you and bless the person who would be kind enough to help me and let me move on.
Every programing language has their own syntax.
Basically, this syntax is acquired from their parent language. The syntax is getting changed in every update.
I have started with swift2 and lot has been changed from swift2 to swift4.
So answering your question,
Answer 1: (_) Underscore --> It is Wildcard pattern
A wildcard pattern matches and ignores any value and consists of an
underscore (_). Use a wildcard pattern when you don’t care about the
values being matched against.
In your example, It means that argument labels are not necessary on invocation of your function.
Learn more about patter at: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html
So if you dont want to write parameter name write:
func makeACake ( _ cash:Double, _ ratio:Double){}
Answer 2: print: Introduce First in swift2, we used to type println in swift.
Now you might ask why println was removed, and the answer is simple "It is no longer required".
Let me take a simple example:
println("Hello")
println("World")
output: (One next line is extra in output, don't know how to format it here)
Hello
World
While with print
print("Hello")
print("World")
output: HelloWorld
Later in swift2 println was deprecated and the same("Next Line") output can be achieved with print itself.
"Evolution"
They are making swift "Easy to Learn", "Easy to code" language
I'm looking for an explanation on playground behavior for Swift. On page 76 of the book Beginning Swift Programming the doSomething function doesn't behave in Xcode as described.
func doSomething(num1: Int, num2: Int) {
println(num1, num2)
}
doSomething(5,6)
The book doesn't show an answer, but I expect a response like (5,6). However, I get no error nor any response. Change the action to println(num1) and doSomething(5,6) works. It produces 5. So does doSomething(5). For that matter. Change it to println((num1, num2)) and doSomething(5,6) yields (5,6).
I'm using Xcode v.6.4 on a Mac running Yosemite. What's going on?
As far as I know, the println() function takes only one parameter.
You either do:
println((num1, num2)) // for printing as a Tuple object
or:
println("\(num1), \(num2)") // for printing as a String object
Reference: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID309
When in a normal project rather than Playground, the code you provided actually works. However, you will have to call doSomething(5, num2: 6) instead. (Swift 1.2/2.0)
It's Swift basic knowledge and suggest you to look up answer rather than ask here. If you want to print something using println with variable. You have to use \(variableName). For example:
println("\(num1), \(num2)")
var test: String
test = "Hello"
count(test)
Error message: Cannot invoke count with an argument list of type '(Int)'
test.characters.count
(Swift 2)
The error makes no sense. Your code works in Xcode 6.3.2, but gives an error in Xcode 7. It must be a Swift 2.0 issue.
If your version of Swift is before 1.2 it could be:
countElement(test)
If the version is 2.0 it is:
test.characters.count
Otherwise try:
Swift.count(test)
I am using openssl for receipt validation in a Swift OS X project. There is one line of code, to extract data from I believe a union, which I do not know how to translate into Swift:
ASN1_OCTET_STRING *octets = pkcs7->d.sign->contents->d.data;
I don't know how to deal with the "->" syntax. What is the Swift translation of this?
I am using the command line to create swift files and using the "swift" command in order to run one of them. However I would like one file to be able to access functions from another file. If this were C then I could use the #include macro and specify where the file is. But Swift's import statement doesn't seem to allow that. There should be a way and I would like to know how to do it.
for example:
If I have a file with a function in it and then I make another file that uses that function. How do I allow it to use it?
// file1.swift
import Foundation
func sayHello() -> String {
return "hello"
}
// file2.swift
import file1 // <-- trying to import file1 but it doesn't work
println(sayHello())
Once the files have been made I then write "swift file2.swift" in terminal. But it tells me..
error: no such module 'file1.swift'
clearly the swift compiler is looking for a module. How do I make file1 into a module? I've seen some solutions but they all take place in Xcode. What I'm looking for is to do it all in the command line.
// file1.swift
func sayHello() -> String {
return "hello"
}
// main.swift
println(sayHello())
and then from the terminal:
$ swiftc file1.swift main.swift
$ ./main
hello
See http://computers.tutsplus.com/tutorials/alfred-workflows-in-swift--cms-21807 for an example of creating a module (an Alfred helper library) from the command line, also http://www.alfredforum.com/topic/4902-alfred-workflows-in-swift-tutorial-is-available/?p=35662 where the author of the tutorial explains the four step compilation process in more detail.