Swift - 下标脚本方法介绍及实例

前端之家收集整理的这篇文章主要介绍了Swift - 下标脚本方法介绍及实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
定义下标脚本之后,可以使用“[]”来存取数据类型的值。

示例1:实现一个我们自定的字符串类,可以方便的通过索引获取某一个字符值,或某一部分字符串。同时也可以通过索引,给某一部分赋值。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class SubString
{
var str: String = ""
init (str: )
{
self .str = str;
}
/**下标脚本:获取/设置部分字符串**/
subscript(start: Int ,length: ) -> String
{
get {
return (str as NSString ).substringWithRange( NSRange (location: start,length: length))
}
set {
let tmp = str
str = ""
s = ""
e = ""
for (idx,item) in tmp.characters. enumerate () {
if (idx < start)
{
s += "\(item)"
} else if (idx >= start + length)
{
e += "\(item)"
}
}
str = s + newValue + e
@H_651_301@}
}
/**下标脚本:获取/设置字符**/
subscript(index: String
{
{
return (str[str.startIndex.advancedBy(index)])
}
{
tmp = str
""
() {
idx == index {
str += "\(newValue)"
} else {
"\(item)"
}
}
}
}
}
str = SubString (str: "hangge.com" )
print (str[7,3]) //获取字符串:com
(str[7]) //获取字符:c
@H_493_403@
str[7,3] = "COM" //设置部分字符串
str[0] = "H" //设置部分字符
(str[0,10]) //Hangge.COM

示例1改进: 通过类扩展,也可以直接给String类添加索引功能代码如下:
51
extension String
String
{
( self {
tmp = self
""
""
() {
(idx < start)
{
"\(item)"
} else {
"\(item)"
}
}
= s + newValue + e
}
}
String
{
[ .startIndex.advancedBy(index)])
{
self
""
() {
idx == index {
+= "\(newValue)"
"\(item)"
}
}
}
}
"hangge.com"
(str[7])
"COM"
"H"
示例2:使用一维数组结合下标方法一定程度上模拟实现了二维数组
29
Matrix {
rows: Int
grid: [ Double ]
(rows: ) {
.rows = rows
.columns = columns
grid = Array (count: rows * columns,repeatedValue: 0.0)
}
func indexIsValidForRow(row: Bool row >= 0 && row < rows && column >= 0 && column < columns
}
subscript(row: {
assert(indexIsValidForRow(row,column: column), "Index out of range" )
grid[(row * columns) + column]
}
{
)
grid[(row * columns) + column] = newValue
}
value = (rows: 20,columns: 20)
value[10,10] = 20
(value[10,10])

1.字符串长度
count(String) -> String.characters.count

2.字符串裁剪

code.substringToIndex(advance(code.startIndex,6)) ->
let endIndex = code.startIndex.advancedBy(6)
code.substringToIndex(endIndex)

3.注册通知

var types = UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert
var acceptAction = UIMutableUserNotificationAction()
acceptAction.identifier = "ACCEPT_IDENTIFIER"
acceptAction.title = "Accept"
acceptAction.activationMode = UIUserNotificationActivationMode.Foreground
acceptAction.destructive = false
acceptAction.authenticationrequired = false

var inviteCategory = UIMutableUserNotificationCategory()
inviteCategory.identifier = "INVITE_CATEGORY"
inviteCategory.setActions([acceptAction],forContext: UIUserNotificationActionContext.Default)
inviteCategory.setActions([acceptAction],forContext: UIUserNotificationActionContext.Minimal)
var categories = NSSet(object: inviteCategory)
var mySettings = UIUserNotificationSettings(forTypes: types,categories: categories as Set<NSObject>)
UIApplication.sharedApplication().registerUserNotificationSettings(mySettings)
UIApplication.sharedApplication().registerForRemoteNotifications()

修改

let acceptAction = UIMutableUserNotificationAction()
acceptAction.identifier = "ACCEPT_IDENTIFIER"
acceptAction.title = "Accept"
acceptAction.activationMode = UIUserNotificationActivationMode.Foreground
acceptAction.destructive = false
acceptAction.authenticationrequired = false

let inviteCategory = UIMutableUserNotificationCategory()
inviteCategory.identifier = "INVITE_CATEGORY"
inviteCategory.setActions([acceptAction],forContext: UIUserNotificationActionContext.Default)
inviteCategory.setActions([acceptAction],forContext: UIUserNotificationActionContext.Minimal)

let categories = NSSet(object: inviteCategory) as! Set<UIUserNotificationCategory>

let mySettings = UIUserNotificationSettings(
forTypes: [.Alert,.Badge,.Sound],
categories: categories)

UIApplication.sharedApplication().registerUserNotificationSettings(mySettings)


UIApplication.sharedApplication().registerForRemoteNotifications()


4 NSError 变成throw

NSJSONSerialization.JSONObjectWithData(data,options: NSJSONReadingOptions(0),error: &error)
修改
try NSJSONSerialization.JSONObjectWithData(NSData(),options: NSJSONReadingOptions(rawValue: 0))
关于try 的用法 请参考 http://www.cocoachina.com/swift/20150831/13238.html
原文链接:https://www.f2er.com/swift/324922.html

猜你在找的Swift相关文章