小编Naf*_*der的帖子

清除Swift中的可选变量

如果我声明一个空图像:

var myImage: UIImage?
Run Code Online (Sandbox Code Playgroud)

然后给它一个值:

myImage = UIImage(named: "drawing.png")
Run Code Online (Sandbox Code Playgroud)

我怎样才能在以后删除该值,将其恢复到原来的空状态?

variables declaration swift

7
推荐指数
1
解决办法
6761
查看次数

将“每次询问”指定为 Siri 意图中参数的默认值

显示 的实例时INUIAddVoiceShortcutViewController,我想让快捷方式的意图参数之一(类型Decimal)默认为Ask Each Time

现在,它默认为文件中的Default Value.intentdefinition。(0如果留空,此字段将重置为。)

有没有办法定义一个意图,这样当它在快捷方式中实例化时,它有一个默认为的参数Ask Each Time

ios sirikit sirishortcuts

7
推荐指数
1
解决办法
400
查看次数

在Swift中使用NSFileManager和createDirectoryAtPath

我正在尝试创建一个新文件夹,但我无法弄清楚如何正确使用createDirectoryAtPath.

根据文档,这是正确的语法:

NSFileManager.createDirectoryAtPath(_:withIntermediateDirectories:attributes:error:)
Run Code Online (Sandbox Code Playgroud)

我试过这个:

let destinationFolder: String = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let deliverablePath: NSURL = NSURL.fileURLWithPath("\(destinationFolder)/\(arrayOfProjectIDs[index])")!
NSFileManager.createDirectoryAtPath(deliverablePath, withIntermediateDirectories: false, attributes: nil, error: nil)
Run Code Online (Sandbox Code Playgroud)

但这给了我错误

调用中的额外参数'withIntermediateDirectories'

我也尝试过很多变化,删除参数等等,但我无法在没有错误的情况下运行它.有任何想法吗?

nsfilemanager ios swift

5
推荐指数
2
解决办法
6582
查看次数

可以为 INIntent 属性赋予值“每次询问”吗?

假设我已经FooIntent.intentdefinition文件中定义了一个名为 的自定义意图。

它有一个bar类型Decimal为的单个参数,其中:
-User can supply value in Siri and Shortcuts app是活动的。
-Default Value0

其自动生成的类定义现在如下所示:

public class FooIntent: INIntent {
    @NSManaged public var bar: NSNumber?
}
Run Code Online (Sandbox Code Playgroud)

如果我构造 a FooIntent,设置barnil,并将其传递给 a INUIAddVoiceShortcutViewController

let intent = FooIntent()
intent.bar = nil
intent.suggestedInvocationPhrase = "Do foo"

let shortcut = INShortcut(intent: intent)

let viewController = INUIAddVoiceShortcutViewController(shortcut: shortcut!)
viewController.delegate = self
window?.rootViewController?.present(viewController, animated: true, completion: nil) …
Run Code Online (Sandbox Code Playgroud)

ios swift sirishortcuts

5
推荐指数
1
解决办法
499
查看次数

循环遍历SKNode的所有孩子?

我有一个带有多个子SKSpriteNodes的SKNode.一个简化的例子:

var parentNode = SKNode()
var childNode1 = SKSpriteNode()
var childNode2 = SKSpriteNode()

self.addChild(parentNode)
parentNode.addChild(childNode1)
parentNode.addChild(childNode2)
Run Code Online (Sandbox Code Playgroud)

我想colorizeWithColor对所有这些孩子采取行动.当我执行操作时parentNode,没有任何效果.

我不能enumerateChildNodesWithName在父母身上使用,因为它的许多孩子已经拥有我正在使用的名字.

有没有办法循环所有孩子parentNode,为了对所有孩子进行单一行动?

ios sprite-kit swift

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

获取对数组的第一个元素(Swift)

我有这个数组:

let arrayOfPairs = [
    (8, 1),
    (6, 4),
    (2, 3),
]
Run Code Online (Sandbox Code Playgroud)

我想要这个数组:

let arrayOfSingleValues = [
    8,
    6,
    2,
]
Run Code Online (Sandbox Code Playgroud)

如何从一对中提取一个值?

arrays swift

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

event.touchesForView().AnyObject()在Xcode 6.3中不起作用

这之前完美无缺:

func doSomethingOnDrag(sender: UIButton, event: UIEvent) {
    let touch = event.touchesForView(sender).AnyObject() as UITouch
    let location = touch.locationInView(sender)
}
Run Code Online (Sandbox Code Playgroud)

但在Xcode 6.3中,我现在得到错误:

无法调用没有参数的'AnyObject'

我该如何解决?

xcode uitouch ios uievent swift

3
推荐指数
1
解决办法
1038
查看次数

在Swift 3/Xcode 8 beta 6中没有调用UIApplicationShortcutItem的AppDelegate函数

Swift 3转换器改变了这条(功能完备)线:

func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
Run Code Online (Sandbox Code Playgroud)

对此:

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
Run Code Online (Sandbox Code Playgroud)

但两者都产生了警告

实例方法'application(:handleActionWithIdentifier:for:completionHandler :)'几乎匹配协议'UIApplicationDelegate'的可选要求'application(:handleActionWithIdentifier:for:completionHandler :)'

并提供制作功能private或添加的解决方案@nonobjc.

无论函数是保留警告,还是恢复为Swift 2语法,还是以建议的方式修复,使用快捷项启动应用程序都不会触发它.

这不是列为已知问题,在这里无论是.有人有想法吗?

xcode uiapplicationdelegate ios swift3 uiapplicationshortcutitem

2
推荐指数
1
解决办法
1028
查看次数

已弃用.WeekdayCalendarUnit的替换

几个星期前我更新了Xcode并开始在这条线上发出警告:

let weekdayComponent = currentCalendar?.components(.WeekdayCalendarUnit, fromDate: date)
Run Code Online (Sandbox Code Playgroud)

'WeekdayCalendarUnit'在iOS 8.0版中已弃用:请改用NSCalendarUnitWeekday

但是当我替换.WeekdayCalendarUnit.NSCalendarUnitWeekday,我得到一个"找不到会员.NSCalendarUnitWeekday"的错误.我尝试了其他一些品种,但没有运气.

我们应该使用什么呢?

xcode nscalendar ios swift

1
推荐指数
1
解决办法
1419
查看次数

在Swift中单行测试nil

我有一个可选的整数,我想将它分配给一个新的变量,如果它不是nil,但如果它是,则指定0.我也想在一条线上做.有一个很好的理由不能做到以下几点?

let output = input != nil ? input! : 0

我觉得像一个更"正确"的方式会是这样的

let output = {
    if let _ = input {
        return input!
    } else {
        return 0
    }
}()
Run Code Online (Sandbox Code Playgroud)

但它更加冗长.

swift

0
推荐指数
1
解决办法
64
查看次数