Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

构建列表与导航 章节 编译报错 #5

Closed
Aprilday opened this issue Nov 21, 2019 · 1 comment
Closed

构建列表与导航 章节 编译报错 #5

Aprilday opened this issue Nov 21, 2019 · 1 comment

Comments

@Aprilday
Copy link

Landmark.swift 这个文件中 ‘ ImageStore’ 报 ‘Use of unresolved identifier 'ImageStore'’

@WillieWangWei
Copy link
Owner

  1. 查看 ../SwiftUI-Tutorials-master/SwiftUI-Essentials/Models/Data.swift 文件是否存在并且引入到了工程中。

  2. 查看 Data.swift 文件中 ImageStore 相关的代码是否正常,即:

final class ImageStore {
    typealias _ImageDictionary = [String: CGImage]
    fileprivate var images: _ImageDictionary = [:]

    fileprivate static var scale = 2
    
    static var shared = ImageStore()
    
    func image(name: String) -> Image {
        let index = _guaranteeImage(name: name)
        
        return Image(images.values[index], scale: CGFloat(ImageStore.scale), label: Text(verbatim: name))
    }

    static func loadImage(name: String) -> CGImage {
        guard
            let url = Bundle.main.url(forResource: name, withExtension: "jpg"),
            let imageSource = CGImageSourceCreateWithURL(url as NSURL, nil),
            let image = CGImageSourceCreateImageAtIndex(imageSource, 0, nil)
        else {
            fatalError("Couldn't load image \(name).jpg from main bundle.")
        }
        return image
    }
    
    fileprivate func _guaranteeImage(name: String) -> _ImageDictionary.Index {
        if let index = images.index(forKey: name) { return index }
        
        images[name] = ImageStore.loadImage(name: name)
        return images.index(forKey: name)!
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants