02-swift基础数据类型

2022-08-02  本文已影响0人  贵叔勇闯天涯
let maximumNumberOfLoginAttempts = 10
var currentLoginAttempt = 0
var x = 0.0, y = 0.0, z = 0.0
var welcomeMessage : String
welcomeMessage = "hello"
welcomeMessage = 10 // Cannot assgin value of type 'Int' to type 'String'
let π = 3.1415926
let 你好 = "你好世界"
let 🐶🐂 = "dogcow"
let π = 3.1415926
let 你好 = "你好世界"

print("欢迎语是\(你好)")
let i = 1
if i {
    printf(i)
}
error: 'Int' is not convertible to 'Bool'

正确为:
if i == 1 {
    printf(i)
}
typealias AudioSample = UInt8
let sample: AudioSample = 32

let error = (1, "没有权限")
print(error.0)
print(error.1)
let error = (errorCode: 1, errorMessage: "没有权限")
print(error.errorCode)
print(error.errorMessage)

Tuple修改

var error = (errorCode: 1, errorMessage: "没有权限")
error.errorCode = 2
error.errorMessage = "2"
<!---->
var error:(Any, String)= (1,  "没有权限")
error.0 = 2
error.0 = "abc"

Tuple分解

let error = (1, "没有权限")
let (errorCode, errorMessage) = error
print(error.errorCode)
print(error.errorMessage)
<!---->
let error = (1, "没有权限")
let (_, errorMessage) = error
print(error.errorMessage)
func writeToFile(content: String) -> (errorCode: Int, errorMessage: String) {
    return(1, "没有权限")
}
var str: String = nil   // 'nil' cannot initalize specified type 'String'
var str1: String? = nil

Optional-If语句以及强制展开

var str: String? = "abc"
let count = str.cont  // 报错
<!---->
var str: String? = "abc"
if str != nil {
    let count = str.cont
    print(count)
}
var str: String?
let count = str!.count // error:Exe...
var str: String? = "abc"
if let actualStr = str {
    let count = actualStr.count
    print(count)
}
var str: String! = "abc"
ley count = str.count
var str: String? = "abc"
let count = str?.count
let lastIndex = count - 1 //报错
<!---->
var str: String? = "abc"
let count = str?.count
if count != nil {
    let lastIndex = count - 1
}

Optional-实现探究

public enum Optional<Wraped> : ExpressibleByNilLiteral {
    
    case none
    case some(Wrapped)
    
    public init(_ some: Wrapped)
}
var str: Optional<String> = "abc"

if let actualStr = str{
    let count = actualStr.count
    print(count)
}
var str: String = "abc"
let count = str.unsafelyUnwrapped.count
print(count)
var emptyString = ""
var anotherEmptyString = String()
if emptyString.isEmpty {
    print("Nothing to see here")
}
let str = "Some string"

let softWrappQuotation = """
the white rabbit put on his spectacles
begin at the begining the kind said gravely, and go on
"""

<!---->

let softWrappQuotation = """
the white rabbit put on his spectacles \
begin at the begining the kind said gravely, and go on \
"""

let softWrappQuotation = """
the white rabbit put on his spectacles \
    begin at the begining the kind said gravely, and go on \
"""
var variableString = "Horse"
variableString += "and carriage"
<!---->
let contantString = "Highlander"
contantString += "and another Highlander"
// this reports a compelee-time error - a constant string cannot be modified
var str: String = "abc"
var str1 = str
print(str == str1)
str += "def"
print(str)
print(str1)
print(str == str1)
for character in "dog!🐶" {
    prfint(character)
}
let multilier = 3
let message = "\(multilier) times 2.5 is \(Double(multilier) * 2.5)"
print(#"Write an interpolated string in Swift using\(multipier)."#)
print(#"6 times 7 is \#(6 * 7)."#)
let greeting = "Guten Tag"
greeting[greeting.startIndex]
<!---->
let greeting = "Guten Tag"
greeting[1] // subscript(_:) is unavailable: cannot subsript
let greeting = "Guten Tag"
greeting[greeting.startIndex]
greeting[greeting.index(before:greeting.endIndex)]
greeting[greeting.index(after:greeting.startIndex)]
let index = greeting.index(greeting.startIndex, offsetBy:7)
greeting[index]
var welcome = "hello"
welcome.insert("!", at:welcome.endIndex)
welcome.insert(contentsOf:" there", at: welcome.index(before:welcome.endIndex))
let greeting = "hello world"
let index = greeting.index(of: ",") ?? greeting.endIndex
let begining = greeting[..<index]
let newString = String(begining)
var welcome = "hello world"
var welcome1 = "hello"
print(welcome == welcome1)
print(welcome.hasPrefix("hello"))
print(welcome.hasSuffix("hello"))

上一篇下一篇

猜你喜欢

热点阅读