Swift基础1_简单数据类型和循环

本文介绍了Swift编程语言的基础概念,包括变量与常量定义、显示指定类型、数组和字典的操作,以及如何创建和初始化数组与字典。此外,还详细讲解了数组添加元素的方法、for循环和switch语句的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 常量变量

var myVariable = 42 //变量
myVariable = 50
let myConstant = 42 //常量

2 显示指明变量类型

let explicitDouble: Double = 70

3 数组和字典

var shoppingList = ["catfish", "water", "tulips", "blue paint"] 
shoppingList[1] = "bottle of water" 
  
var occupations = [ 
    "Malcolm": "Captain", //前面是key 后面是value
    "Kaylee": "Mechanic", 
] 
occupations["Jayne"] = "Public Relations"

4 创建初始化数组和字典

let emptyArray = String[]() 
let emptyDictionary = Dictionary<String, Float>() 


5数组添加

var emptyarry = String[]()
emptyarry.append("dfd")


6 for循环

let sss = [11,12,43,554,12,434]
for i in 0..4 {
     print("\(i) = \(sss[i])");
         print("\n");
      }
for i in sss{
      print(i);
         print("\n");
      }

7switch

let vegetable = "red pepper" 
switch vegetable { 
case "celery": 
    let vegetableComment = "Add some raisins and make ants on a log." 
case "cucumber", "watercress": 
    let vegetableComment = "That would make a good tea sandwich." 
case let x where x.hasSuffix("pepper"): 
    let vegetableComment = "Is it a spicy \(x)?" fallthrough 
default: 
    let vegetableComment = "Everything tastes good in soup." 
} 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值