swift構文ベース

6343 ワード

変数と定数の宣言
//1.      :Int   ,Double  Float     ,Bool   ,String     ,Array   ,Dictionary   
//2.   let ,  var
let con = 100
var avi = 30
avi = 40
//3.             ,     
var a = 3, b = 4, c = 5
//4.    :            ,        
var who: String
who = "xiaoming"
print(who)

swiftの基本構文
タイプ変換
//    
let a: UInt8 = 10
let b: UInt16 = 110
print("\(UInt16(a) + b)")

//        
let sa = 3
let pi = 3.1415
let add = Double(sa) + pi
print(add)

メタグループ
//     :             ,             
let people = (18, "xiaoming")
//                  
let (age, name) = people
print(age)
print(name)

//              ,                  
let (age1, _) = people
print(age1)

//               
print(people.0)

//                  ,          
let rec = (w: 10, h: 20)
print(rec.w)

オプションのタイプ
//    :          ,               
let str : String? = "12345"

// if           
if str != nil {
    print(str!)//    !          ,            
}

//                    ,              ,         
/*
        if while               ,           
    : swift nil          ,  oc                
 */
if let str1 = str {
    print(str1)
}
var  servercode : Int? = 404
servercode = nil //  servercode     
var sunny : String?

//        :             nil ,                
//        ,          nil
var possiblestr: String! = "swift"
print(possiblestr)//     !       

断言する
//    ,            ,         ,                 ,            
let age2 = -10
assert(age2>=0, "     0")//assert       ,         ,            

演算子
//2.     :         &   &+,&-, &*
var c = UInt8.min
c = c &- 1
print(c)

//swift        ,  oc      
var rem = 10 / 2.3
print(rem)

//      (a ?? b  a       ,a  nil  a   ,    b   )
/*
    a       
    b       a          
 */
let words = "hello"
var say: String?
var content = say ?? words
print(content)

//      a...b        a..

文字列
//   
var str1 = "swift"
var str2 = String()//         
var str3 = str1 + "  "//          + 
print(str3)
str1 += "  "//+=         
print(str1)

let char : Character = "!"
str1.append(char)
print(str1)//append    
print("str3 has \(str3) chars")

let quotation = "same"
let samequ = "same"
if quotation == samequ {//         
    print("same")
}

var food = ["fruits:apple",
            "fruits:orange",
            "fruits:banana",
            "vegetable:tomato",
            "vegetable:potato"]
for fru in food {
    if fru.hasPrefix("fruits"){//hasPrefix       
      print(fru)
    }
    if fru.hasSuffix("apple"){//hasSuffix           
        print(fru)
    }
}

はいれつ
//            ,           。        ,        。
food.append("vegetable:aaaa")//  append       
food[0...2] = ["ss", "nn"]//               。       3 ,     2  , swift      nil
print(food)

food.insert("meat", at: 0)//  insert    
food.remove(at: 0)//  remove            
food.removeLast()//         

//                      
var someint = [Int]()
print("someints is of type [int] with\(someint.count)items")

//                         :   ,3 
var threedouble = [Double](repeating: 0.0, count: 3)


辞書
//  
/*
 swift                       ,        ,                  
 */
var person = ["age": 18,
                "name": "jack"] as [String : Any]

/*updateValue                           。             ,
               
*/
if let oldname = person.updateValue("tom", forKey: "name") {
    print("    :\(oldname)")
}

/*
 2.removeValue                
 3.            (key, value)       ,                      
 */
for (key, value) in person{
    print(key,value)//        ,           
}

var dic = Dictionary()//     
dic[16] = "  "//   16,     
print(dic)


dic = [:]//         

せいぎょりゅう
//   
let base = 3
let power = 10
var answer = 1
for _ in 1...power {//     _(        )        ,              
    answer *= base
}
print(answer)

// switch case     break
let count = 3_000_000_000
let countedthings = "stars in the milky way"
var naturalcount : String
switch count {
case 0:
    naturalcount = "no"
case 1...3:
    naturalcount = "a few"
case 4...9:
    naturalcount = "several"
case 10...99:
    naturalcount = "tens of"
case 100...999:
    naturalcount = "hundreds of"
case 1000...9999:
    naturalcount = "thounds of"
default:
    naturalcount = "millions and millions of"
}

関数#カンスウ#
//  
//       :     ,       
func sayhello(personname: String) -> String{
    return "hello" + personname + "!"
}

//    
print(sayhello(personname: "anna"))

//  2          ,       
func lengthnumber(start: Int, end: Int) -> Int{
    return end - start
}
print(lengthnumber(start: 1, end: 10))

/*
         personname,         ,              , 
       。
 */
func join(s1: String, toString s2: String, withjoiner joiner: String) -> String {
    return s1 + s2 + joiner
}
print(join(s1: "hello", toString: "swift", withjoiner: "!"))

/*
 joiner       ,        (""),              
     ,swift         。  ,                 
 */


/*
              void。  swift ,void         
 */

func addtwoints(a: Int, b: Int) -> Int{
    return a + b
}

func multiplytwoints(a: Int, b: Int) -> Int{
    return a*b
}

/*
       mathfunction   ,   ‘   2 int      ,     int      ’
 */
var mathfunction: (Int,Int) -> Int = addtwoints
print("results:\(mathfunction(2, 3))")


/*
          
 */
func printmathresult(mathfunction: (Int, Int) -> Int, a: Int, b: Int){
    print(mathfunction(a, b))
}
print(printmathresult(mathfunction: addtwoints, a: 3, b: 5))

クラス#クラス#
// 
// class       
class person {
    //    
    var name = "tina"
    var age = 19
    
    //    
    func produce(){
        age += 1
    }
}

補足:構造体と列挙でメソッドを定義することもできます.