Swift notes and useful resources
swift(ios dev..)
- we can declare constants using let kw.
- we can declare variables using var kw.
- we can declare multiple cons. or varls. on a single using commas,
- EX==
- let kj=20 //constant
- var dj=30 //variable
- var x=3.4,y=43,z=43.4
- Swift assigns each variable and constant a type based on what value it’s given when it’s created. So, when you write code like this Swift can see it holds a string.
- let str = "Hello, playground"
- //That will make str a string, so you can’t try to assign it an integer or a boolean later on. This is called type inference: Swift is able to infer the type of something based on how you created it.
IMPORTANT➖
If you want you can be explicit about the type of your data rather than relying on Swift’s type inference, like this:
var album: String
let year: Int = 1989
let height: Double = 1.78
let TaylorRocks: Bool = true
//Notice that booleans have the short type name Bool, in the same way, that integers have the short type name Int.
//Strings start and end with double quotes, but if you want them to run across multiple lines you should use three sets of double-quotes.
/ex
var str1 = """
This goes
over multiple
lines
"""
//colon in the declaration means ".. type of .." code can be read as-- ##@ "Declare a variable Album as a string" //From the above example
//now Without any error
album=“hello”
Integers hold whole numbers, doubles hold fractional numbers, and booleans hold true or false.
- String interpolation allows you to create strings from other variables and constants, placing their values inside your string.
- Swift uses type inference to assign each variable or constant a type, but you can provide explicit types if you want.
Constant and variable cannot contain whitespace characters, mathematical symbols, a private use Unicode scalar values, box or a line or box characters nor Cannot begin with a number
- string interpolation – the ability to place variables inside your strings to make them more useful.
- You can place any type of variable inside your string – all you have to do is write a backslash, \, followed by your variable name in parentheses
var score = 85
var str = "Your score was \\(score)"
basic swift
step 1
For commenting short cut is commend + /
in swift programming language, we use camel casing ex - basicSwiftNotes (Here is the first letter is small and the starting of next to 2 letters are capital)
To include something mathematical or some variable in between the print statement we have to keep \ and write our operations in ()
print("text \(8403+903) text")== called string Interpolation
print("hello \\(2+4) world") //op- //hello 5 world print("result of 5+3 = \\(5+3)")
- continues
Comments
Post a Comment