Swift扩展分离文件

时间:2017-03-09 06:45:48

标签: ios swift uiimageview swift-extensions

我想在ViewController中使用#include "hello.pb.h" #include<fstream> #include<iostream> using namespace std; int main() { fstream fo("./hello.data",ios::binary|ios::out); hello p1,p2,p3; p1.set_f1(1); p1.set_f2(2); p2.set_f1(3); p2.set_f2(4); p3.set_f1(5); p3.set_f2(6); p1.SerializeToOstream(&fo); p2.SerializeToOstream(&fo); p3.SerializeToOstream(&fo); fo.close(); fstream fi("./hello.data",ios::binary|ios::in); hello pi; pi.ParseFromIstream(&fi); cout<<pi.f1()<<pi.f2()<<endl; return 0; } 扩展名中定义的方法。当扩展程序与视图控制器位于同一文件中时,它会起作用 - 我想将其移动到它自己的单独/公共文件UIImageView。但是当我这样做时,我的视图控制器中出现错误

UIImageView+Download.swift

这是扩展程序:

Value of type 'UIImageView' has no member 'downloadFrom'

它在视图控制器中的用法:

import Foundation
import UIKit

public extension UIImageView {

    public func downloadFrom(urlString: String) {
        guard let url = URL(string: urlString) else { return }
        downloadFrom(url: url)
    }

    public func downloadFrom(url: URL) {
        contentMode = .scaleAspectFit
        URLSession.shared.dataTask(with: url) { data, response, error in
            guard let data = data, error == nil else { return }
            DispatchQueue.main.async {
                self.image = UIImage(data: data)
            }
        }.resume()
    }
}

0 个答案:

没有答案