swift入门仿写项目及遇坑出坑

2017-05-27  本文已影响130人  SPIREJ
首页 理财 我的
第一个坑,使用pod建立工程时终端出错
pod[!] Unable to add a source with url `https://github.com/CocoaPods/Specs.git` named `master`.You can
 try adding it manually in `~/.cocoapods/repos` or via `pod repo add`.

解决方案:终端输入以下命令

pod repo add master https://github.com/CocoaPods/Specs.git

然后提示执行

pod setup

如果出现

pod[!] The `master` repo is not a git repo.

进入/users/你的用户名/.cocoapods/repos,删除master文件夹然后执行,如我这么进入

cd ~/.cocoapods/repos/

master文件夹是个目录,删除时需要连下面的分支一起删掉

rm -rf ~/.cocoapods/repos/master

删除master之后,再pod setup,之后配置好你的Podfile,最后pod install就成功的建立了 同名的.xcworkspace工程了

pod setup
#先 cd 到你工程的目录
pod init
open -a Xcode Podfile
#添加你需要的三方库
pod install
sudo xcode-select -switch /Applications/Xcode.app/

在终端里输入下方命令可以知道Xcode的路径:

xcode-select -p
第二个坑,加载基于XIB创建的自定义控件时报错
fatal error: init(coder:) has not been implemented: file 
/路径/XXX.swift, line 43
(lldb)
class XSCustomView: UIView {
    
    var contentView:UIView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView = loadFromNib()
        addSubview(contentView)
    }
    
    func loadFromNib() -> UIView {
        return Bundle.main.loadNibNamed("XSCustomView", owner: nil, options: nil)?.first as! UIView
    }
    
    override func layoutSubviews() {
        contentView.frame = bounds
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        fatalError("init(coder:) has not been implemented")
    }
}
上一篇 下一篇

猜你喜欢

热点阅读