标签: swift2

Swift 2.0 - 二进制运算符"|" 不能应用于两个UIUserNotificationType操作数

我试图以这种方式注册本地通知的应用程序:

UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil))
Run Code Online (Sandbox Code Playgroud)

在Xcode 7和Swift 2.0中 - 我收到错误Binary Operator "|" cannot be applied to two UIUserNotificationType operands.请帮我.

ios swift swift2

191
推荐指数
3
解决办法
5万
查看次数

Swift的守卫关键词

Swift 2引入了guard关键字,可用于确保准备好配置各种数据.我在这个网站上看到的一个例子演示了一个submitTapped函数:

func submitTapped() {
    guard username.text.characters.count > 0 else {
        return
    }

    print("All good")
}
Run Code Online (Sandbox Code Playgroud)

我想知道使用guard是否与使用if条件的旧式方式有所不同.它是否会带来好处,而使用简单的支票是无法获得的?

swift2 guard-statement

190
推荐指数
8
解决办法
11万
查看次数

二元运算符'|' 不能应用于两个UIViewAutoresizing操作数

在Swift 2.0中获取此错误.

二元运算符'|' 不能应用于两个UIViewAutoresizing操作数

这是代码:

let view = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 568))
addSubview(view)
view.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
Run Code Online (Sandbox Code Playgroud)

知道可能是什么问题吗? 在此输入图像描述

cocoa-touch ios swift swift2

186
推荐指数
4
解决办法
4万
查看次数

试试!试试?有什么区别,什么时候使用?

Swift 2.0中,Apple引入了一种处理错误的新方法(do-try-catch).几天前在Beta 6中引入了更新的关键字(try?).另外,知道我可以使用try!.3个关键字之间有什么区别,何时使用?

error-handling swift swift2 swift3

162
推荐指数
1
解决办法
4万
查看次数

Swift do-try-catch语法

我尝试了解swift 2中的新错误处理事情.这是我做的:我首先声明了一个错误枚举:

enum SandwichError: ErrorType {
    case NotMe
    case DoItYourself
}
Run Code Online (Sandbox Code Playgroud)

然后我宣布了一个抛出错误的方法(不是一个例外的人.这是一个错误.).这是方法:

func makeMeSandwich(names: [String: String]) throws -> String {
    guard let sandwich = names["sandwich"] else {
        throw SandwichError.NotMe
    }

    return sandwich
}
Run Code Online (Sandbox Code Playgroud)

问题来自呼叫方.以下是调用此方法的代码:

let kitchen = ["sandwich": "ready", "breakfeast": "not ready"]

do {
    let sandwich = try makeMeSandwich(kitchen)
    print("i eat it \(sandwich)")
} catch SandwichError.NotMe {
    print("Not me error")
} catch SandwichError.DoItYourself {
    print("do it error")
}
Run Code Online (Sandbox Code Playgroud)

do行编译器说完之后Errors thrown from here are not handled because the enclosing …

swift swift2

157
推荐指数
2
解决办法
12万
查看次数

使用@testable时,"模块未编译用于测试"

我正在尝试使用Swift 2的新@testable声明来将我的类暴露给测试目标.但是我收到这个编译错误:

在此输入图像描述

Intervals是包含我试图公开的类的模块.我该如何摆脱这个错误?

unit-testing ios swift swift2

138
推荐指数
6
解决办法
2万
查看次数

"++"和" - "运算符已弃用Xcode 7.3

我正在看Xcode 7.3笔记,我注意到了这个问题.

++和 - 运算符已被弃用

有人可以解释为什么它被弃用了吗?我是对的,在Xcode的新版本中,你现在要使用而不是++这个x += 1;

例:

for var index = 0; index < 3; index += 1 {
    print("index is \(index)")
}
Run Code Online (Sandbox Code Playgroud)

警告的屏幕截图

increment decrement swift swift2 swift3

136
推荐指数
7
解决办法
7万
查看次数

从swift中的字符串中获取整数值

所以我可以这样做:

var stringNumb: NSString = "1357"

var someNumb: CInt = stringNumb.intValue
Run Code Online (Sandbox Code Playgroud)

但我无法找到方法来做到这一点String.我想做点什么:

var stringNumb: String = "1357"

var someNumb: Int = Int(stringNumb)
Run Code Online (Sandbox Code Playgroud)

这也不起作用:

var someNumbAlt: Int = myString.integerValue
Run Code Online (Sandbox Code Playgroud)

swift swift2

129
推荐指数
5
解决办法
16万
查看次数

stringByAppendingPathComponent不可用

我的应用程序在Instagram上分享照片,为此,首先将其保存在临时目录中:

let writePath = NSTemporaryDirectory().stringByAppendingPathComponent("instagram.igo")
Run Code Online (Sandbox Code Playgroud)

它正在努力Swift 1.2,但不起作用Swift 2.0.

给出错误消息是:

stringByAppendingPathComponent不可用:改为使用NSURL上的URLByAppendingPathComponent.

ios swift swift2

127
推荐指数
6
解决办法
6万
查看次数

在Swift 2中使用自定义消息抛出错误/异常的最简单方法?

我想在Swift 2中做一些我以前用其他多种语言做的事情:用自定义消息抛出运行​​时异常.例如(在Java中):

throw new RuntimeException("A custom message here")
Run Code Online (Sandbox Code Playgroud)

我知道我可以抛出符合ErrorType协议的枚举类型,但我不想为我抛出的每种类型的错误定义枚举.理想情况下,我希望能够尽可能地模仿上面的例子.我研究了创建一个实现ErrorType协议的自定义类,但我甚至无法弄清楚该协议需要什么(参见文档).想法?

ios swift swift2

125
推荐指数
8
解决办法
8万
查看次数