• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Swift学习笔记第一天-变量常量,及数据类型

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

1.定义变量

 用关键字 var

比如 var i=2

2.定义常量用let

 如let c=3

 可见Swift 定义时不用指定类型。由编译器推断

  如果想指定类型

 var i:Int32=2

 练习

let apples=3

var h:Int32=32

h=32

let cs="i love \(apples + h)as"

cs 得结果

会发现程序报错。a

could not find member ''convertfromstringInterpolationSegment"

 由此可见Swift  会将默认将类型推断为双精度

正确程序为

 

let apples:Int32=3

 

var h:Int32=32

 

h=32

 

let cs="i love \(apples + h)as"

 

2 .定义数组

  

var shoppingList=["haha","as",3,32.00]

可见数组中得元素类型可以不同。

将数组元素赋给定义得一个变量

var s= shoppingList[0]


会发现报错
错误如下
prefix /postfix=is reserved
这个错误我开始以后是var只能定义值类型得原因
google才发现很有意思得一个错误

 

2down voteaccepted

Add a space after the =. (=[ looks too sad to be an operator.) It's probably seeing =value as a use of a (possible, but not implemented) prefix operator.

Swift isn't entirely whitespace-agnostic like C... in particular, it uses whitespace to distinguish prefix from postfix operators (because ++i++ in C is a grammar oddity). But it's not ridiculously strict about whitespace like Python either.

 =号前后空格是有一侧存在得是保留运算附

 

修改为

var s:String=shoppingList[0]

会发现报错

anyObject is not  转换成string 

可见数组是指针引用类型

正确写法是

var s:String=shoppingList[0] asNSString

定义字典

 

var occupations=[

    "Malcolm": "Captain",

    "Kaylee": "Mechanic",

]

occupations["Jayne"] = "Public Relations"

把字典中value得类型变化下

var occupations=[

    "Malcolm": "Captain",

    "Kaylee": 2,

]

occupations["Jayne"] = "Public Relations"

会发现报错

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
swift-本地通知发布时间:2022-07-14
下一篇:
Swift协议+代理发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap