小编Wak*_*Wak的帖子

swift上的UIImage无法检查是否为零

我在Swift上有以下代码

var image = UIImage(contentsOfFile: filePath)
        if image != nil {
           return image
       }
Run Code Online (Sandbox Code Playgroud)

它过去工作得很好,但现在在Xcode Beta 6上,它会返回一个警告

 'UIImage' is not a subtype of 'NSString'
Run Code Online (Sandbox Code Playgroud)

我不知道该怎么做,我尝试了不同的东西

 if let image = UIImage(contentsOfFile: filePath) {
            return image
   }
Run Code Online (Sandbox Code Playgroud)

但错误变为:

Bound value in a conditional binding must be of Optional type
Run Code Online (Sandbox Code Playgroud)

这是Xcode6 beta 6上的错误还是我做错了什么?

xcode uiimage ios swift

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

Swift完成块

我很难理解我遇到的问题.为简化起见,我将使用UIView方法.基本上,如果我写的方法

UIView.animateWithDuration(1, animations:  {() in
        }, completion:{(Bool)  in
            println("test")
    })
Run Code Online (Sandbox Code Playgroud)

它工作正常.现在,如果我使用相同的方法,但创建一个这样的字符串:

    UIView.animateWithDuration(1, animations:  {() in
        }, completion:{(Bool)  in
            String(23)
    })
Run Code Online (Sandbox Code Playgroud)

它停止工作.编译器错误:在调用中缺少参数'delay'的参数

现在,这是奇怪的部分.如果我执行与失败的代码完全相同的代码,但只需添加如下的打印命令:

   UIView.animateWithDuration(1, animations:  {() in
        }, completion:{(Bool)  in
            String(23)
            println("test")
    })
Run Code Online (Sandbox Code Playgroud)

它又开始起作用了.

我的问题基本上是一回事.我的代码:

   downloadImage(filePath, url: url) { () -> Void in
         self.delegate?.imageDownloader(self, posterPath: posterPath)
        }
Run Code Online (Sandbox Code Playgroud)

不行.但如果我换到.

 downloadImage(filePath, url: url) { () -> Void in
             self.delegate?.imageDownloader(self, posterPath: posterPath)
                println("test")
            }
Run Code Online (Sandbox Code Playgroud)

甚至:

downloadImage(filePath, url: url) { () -> Void in
             self.delegate?.imageDownloader(self, posterPath: posterPath)
             self.delegate?.imageDownloader(self, posterPath: posterPath)
            }
Run Code Online (Sandbox Code Playgroud)

它工作正常.我不明白为什么会这样.我接近它接受它是一个编译器错误.

block ios swift xcode6

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

Swift ARC和块

我正在尝试一个简单的例子,如下所示:https: //developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html#//apple_ref/doc/uid/TP40014097-CH20 -XID_88

这是我的代码.(忽略其他可能的代码,这是一个空项目,此代码写在空的UIViewcontroller viewDidLoad中)

    dispatch_async(dispatch_get_main_queue()) {
        [unowned self] in
        println(self)
    }
Run Code Online (Sandbox Code Playgroud)

我不明白为什么当我运行专业版时它会崩溃

  • 线程#1:tid = 0x1a796,0x00284d18 libswiftCore.dylib`_swift_release_slow + 8,queue ='com.apple.main-thread',stop reason = EXC_BAD_ACCESS(code = 1,address = 0x458bc681)

在最新的测试版(5)上有什么变化,这是不再支持的吗?谢谢

编辑:有趣的是这段代码适用于Objc

__weak MyViewController *weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
    NSLog(@"%@", weakSelf);
});
Run Code Online (Sandbox Code Playgroud)

edit2:关于这个链接的解释:我们总是在Swift中使用[无主自我]内部关闭弱和无主的区别是错误的.

这不仅仅是弱小的nils而且是无主的.如果是这种情况,这也应该崩溃:

  dispatch_async(dispatch_get_main_queue()) {
            [weak self] in
            println(self)
        }
Run Code Online (Sandbox Code Playgroud)

但它没有,它打印指针,所以,它不是零.

ios automatic-ref-counting swift

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

Xcode Beta 6.1和Xcode 6 GM因为奇怪的原因而陷入索引

我正在开发一个快速的应用程序,在某些时候我有一个类似于此的代码:

 import UIKit

class ViewController: UIViewController {
    private var a: UIImageView!
    private var b: UIImageView!
    private var c: UILabel!
    private var d: UILabel!
    private var e: UILabel!
    private var f: UILabel!
    private var g: UIView!
    private var h: UIView!
    private var i: UIView!
    private var j: UIView!
    private var k: UIImageView!
    private var l: UIView!
    private var m: UIView!
    private var n: UIView!
    private var o: UIView!
    private var p: UIScrollView!
    private var q: UIView!

    override func viewDidLoad() {
        super.viewDidLoad() …
Run Code Online (Sandbox Code Playgroud)

xcode swift xcode6

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

SwiftUI 文本对齐与 maxWidth 不起作用

我有以下代码:

struct ContentView: View {
    var body: some View {
        Text("Test")
            .foregroundColor(.white)
            .font(.subheadline)
            .frame(maxWidth: .infinity)
            .alignmentGuide(.leading) { d in d[.leading] }
            .background(Color(.blue))
    }
}
Run Code Online (Sandbox Code Playgroud)

但是正如您在下面的图像中看到的那样,它不会左对齐文本。有谁知道为什么会这样?也许是因为我使用的是 maxWidth,alignmentGuide 认为它已经左对齐了?

结果

text alignment swift swiftui

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