相关疑难解决方法(0)

Xcode 8/Swift 3:"UIViewController类型的表达式未使用"警告

我有以下功能,以前干净地编译,但使用Xcode 8生成警告.

func exitViewController()
{
    navigationController?.popViewController(animated: true)
}
Run Code Online (Sandbox Code Playgroud)

"表达式类型"UIViewController?"未使用".

为什么要说这个,有没有办法删除它?

代码按预期执行.

ios swift

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

调用[myFunction]的结果未使用

在Obj-C中,常见的做法是使用便捷函数来执行常见操作,例如为视图配置自动布局:

func makeConstraint(withAnotherView : UIView) -> NSLayoutConstraint
{
   // Make some constraint 
   // ...

   // Return the created constraint
   return NSLayoutConstraint()
}
Run Code Online (Sandbox Code Playgroud)

如果你只需要设置约束并忘记它,你可以调用:

[view1 makeConstraint: view2]

如果您想稍后存储约束以便可以删除/修改它,您可以执行以下操作:

NSLayoutConstraint * c;
c = [view1 makeConstraint: view2]
Run Code Online (Sandbox Code Playgroud)

我想在swift中执行此操作,但如果我调用上面的函数并且不捕获返回的约束,我会收到警告:

Result of call to 'makeConstraint(withAnotherView:)' is unused
Run Code Online (Sandbox Code Playgroud)

很烦人.有没有办法让Swift知道我并不总是想要捕获返回值?

注意:我知道这一点.这很难看,而不是我想要的:

_ = view1.makeConstraint(withAnotherView: view2)
Run Code Online (Sandbox Code Playgroud)

function swift

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

从Swift中的数组中获取随机元素

我有一个像这样的数组:

var names: String = [ "Peter", "Steve", "Max", "Sandra", "Roman", "Julia" ]
Run Code Online (Sandbox Code Playgroud)

我想从该数组中获取3个随机元素.我是从C#来的,但是我很快就不知道从哪里开始.我想我应该首先将阵列洗牌,然后从中挑选前3个项目?

我尝试使用以下扩展名对其进行随机播放:

extension Array
{
    mutating func shuffle()
    {
        for _ in 0..<10
        {
            sort { (_,_) in arc4random() < arc4random() }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但它然后说"'()'在"shuffle()"的位置不能转换为'[Int]'".

为了挑选我使用的一些元素:

var randomPicks = names[0..<4];
Run Code Online (Sandbox Code Playgroud)

到目前为止看起来不错.

怎么洗牌?或者有没有人有更好/更优雅的解决方案?

arrays random shuffle swift

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

Swift Compiler Warning : Result of call to 'save(defaults:)' is unused

So my table view is not loading anything and I think it's because of this warning that I get. It saids the save function is not being used so how can it load something that is not saved. What I am saving is the indexPath and Section of the row that the user selected via a button action in the row.

Warning:

Result of call to 'save(defaults:)' is unused

Code:

func saveSorting(_ dataIdBlock: (Any) -> String) {

    guard let items …
Run Code Online (Sandbox Code Playgroud)

warnings nsuserdefaults nscoding ios swift

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

标签 统计

swift ×4

ios ×2

arrays ×1

function ×1

nscoding ×1

nsuserdefaults ×1

random ×1

shuffle ×1

warnings ×1