小编hop*_*opy的帖子

SwiftUI 无法通过 UIColor(patternImage:) 创建颜色

我想从图案图像创建颜色对象,但它似乎不起作用:

\n\n
struct ContentView: View {\n\n    var imageColor:Color{\n        let image = UIImage(named: "image_name")!\n        let uiColor = UIColor(patternImage: image)\n        return Color(uiColor)\n    }\n\n    var body: some View {\n        Text("Hello World")\n            .background(imageColor)\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

运行代码,文本背景看起来是透明的。

\n\n

所以我在 lldb 中使用 po 命令来查看 imageColor 到底是什么,我得到了这个:

\n\n
po imageColor\n\xe2\x96\xbf <UIDynamicPatternColor: 0x283b90540; image = <UIImage:0x2809a8090 named(main: bwBlocks2) {509, 300}>>\n\xe2\x96\xbf provider : <ColorBox<UIColor>: 0x283b905c0>\n
Run Code Online (Sandbox Code Playgroud)\n\n

看起来它从图像中创建了正确的颜色,但我在文本背景中看不到它!

\n\n

这是怎么回事?\n图像尺寸有问题吗?\n谢谢;)

\n

swift swiftui

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

为什么空发布器不触发组合中的完成事件?

我们知道Combine中的Empty Publisher会立即触发完成事件\xef\xbc\x9a

\n
Empty<Void,Never>()\n    .sink {\n        print("completion: \\($0)")  // will print!\n    } receiveValue: {}\n
Run Code Online (Sandbox Code Playgroud)\n

但是 flatMap 返回的 Empty Publisher 不会触发完成事件:

\n
var subs = Set<AnyCancellable>()\nlet p0 = PassthroughSubject<[Int],Error>()\nlet p1 = p0\n    .flatMap {_ in\n        Empty<Void,Never>() // same Empty Publisher\n    }.eraseToAnyPublisher()\n\np1\n    .sink {\n        print("completion: \\($0)")  // but NOT print!\n    } receiveValue: {}\n    .store(in: &subs)\n\np0.send([1,2,3])\n
Run Code Online (Sandbox Code Playgroud)\n

这是为什么???我是不是错过了什么???谢谢!;)

\n

publisher swift combine

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

具有关联类型错误的Swift协议

我创建了一个函数类:Bar,Bar使用属于它的委托做特定的事情,这个委托符合协议FooDelegate,类似的东西:

protocol FooDelegate{
    associatedtype Item

    func invoke(_ item:Item)
}

class SomeFoo:FooDelegate{
    typealias Item = Int

    func invoke(_ item: Int) {
        //do something...
    }
}

class Bar{
    //In Bar instance runtime ,it will call delegate to do something...
    var delegate:FooDelegate!
}
Run Code Online (Sandbox Code Playgroud)

但是在课堂上Bar:var delegate:FooDelegate!我收到了一个错误:

协议'FooDelegate'只能用作通用约束,因为它具有Self或相关类型要求

我怎么能解决这个问题?

generics swift swift-protocols

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

标签 统计

swift ×3

combine ×1

generics ×1

publisher ×1

swift-protocols ×1

swiftui ×1