記錄本月開發遇到的知識點,小tips,和bug總結。
大事件
新版iPad Pro、MacBook Air、Mac mini發布,全線漲價,但是真香。。。
Tips
適配swift4.2
1、利用xcode快速遷移
升級到Xcode10之后,我們打開項目會出現如下提示,
點擊會有一個版本升級窗口,如果你的的項目包含一些第三方庫的話,第三方庫的選型也會出現在上面:
默認勾選第三方庫,但是我們適配的時候不應該讓Xcode去自動檢索第三方庫代碼。只對我們的app進行代碼遷移就夠了。
適配完后可以在這里查看我們當前的swift版本:
對于第三方庫,如果都適配了swift4.2,那么更新到對應版本就行了。如果有沒適配的,可以通過制定版本的方式解決沖突,在Podfile文件末尾添加如下代碼:
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['SWIFT_VERSION'] = '4.0'
end
end
end
屬性訪問權限
swift中提供的訪問權限關鍵詞由低到高有以下五種:
private < fileprivate < internal < public < open
其中internal是Swift中的默認控制級,一下介紹了這幾種關鍵字的區別:
- private:當前作用域或者當前類中訪問。
- fileprivate:表示代碼可以在當前文件中訪問。
- internal:在當前target中訪問
- public:可跨target使用,但不能被集成或者重寫。
- open:可跨target使用,允許被集成或者重寫。
對于一個嚴格的項目來說,精確的最小化訪問控制級別對于代碼的維護來說是很重要的。能用public的就別用open。
修改文件權限
當我們執行某些命令行操作,收到如下提示時:
Linking /usr/local/Cellar/the_silver_searcher/2.1.0...
Error: Could not symlink etc/bash_completion.d/ag.bashcomp.sh
/usr/local/etc/bash_completion.d is not writable.
就表明我們對需要修改的文件權限不夠,我們可以給文件加上修改的權限:
sudo chown -R $(whoami) /usr/local/etc/bash_completion.d
其中-R
表示對目前目錄下的所有文件與子目錄進行相同的擁有者變更(即以遞回的方式逐個變更)
默認關鍵字
public static let `default` = ImageCache(name: "default")
default是默認關鍵字,如果使用要加單斜號。
tabbar手動跳轉
func tabBarController(UITabBarController, didSelect: UIViewController)
In iOS v3.0 and later, the tab bar controller calls this method regardless of whether the selected view controller changed. In addition, it is called only in response to user taps in the tab bar and is not called when your code changes the tab bar contents programmatically.
In versions of iOS prior to version 3.0, this method is called only when the selected view controller actually changes. In other words, it is not called when the same view controller is selected. In addition, the method was called for both programmatic and user-initiated changes to the selected view controller.
tabbar的didSelect代理方法,只有在手動點擊的時候才會觸發。通過代碼跳轉是不會觸發的。
自定義tabbar動畫
// 當點擊tabBar的時候,自動執行該代理方法(不需要手動設置代理)
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
for view in tabBar.subviews {
//控制view實現各種動畫效果
}
}
NS_ASSUME_NONNULL_BEGIN 和 NS_ASSUME_NONNULL_END
從xcode6.3開始,為了能讓OC也能表示swift的?(optional)和!功能,增加了對對象的可選指定。指定屬性是否可選,可以:
@property (nonatomic, copy, nonnull) NSString * tickets;
//或者
@property (nonatomic, copy) NSString * __nonnull tickets;
如果屬性多了,每一個都這么寫會很麻煩,蘋果增加了一對新的宏命令,就是NS_ASSUME_NONNULL_BEGIN
和NS_ASSUME_NONNULL_END
。放在里面的對象就相當于都增加了nonnull
命令,nonull
為默認值。
一個自定義collectionView布局的bug
bug描述:
NSInternalInconsistencyException
UICollectionView received layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0x280d2b200> {length = 2, path = 1 - 5}
原因:
layoutAttributesForElementsInRect
返回的UICollectionViewLayoutAttributes
數組有indexPath
沒有被 [NSIndexPath indexPathForRow:numberOfSection]
覆蓋。
當數據量增加時不會出問題,當數量減少時出現。有人反映這是iOS10的bug,但實際上,我拿iOS10模擬器跑并沒有問題,反而是在崩潰后臺看到是iOS12的用戶上報的。那究竟什么原因不詳,附stackoverflow(iOS 10 bug: UICollectionView received layout attributes for a cell with an index path that does not exist - Stack Overflow]地址。
解決方案:
當我們自定義layout時,需要清除UICollectionViewLayoutAttributes的緩存
//方案一: 在自定義layout的類里
override func prepare() {
super.prepare()
attributesArr.removeAll()
}
//方案二:在collectionview刷新出
collectionView.reloaData()
collectionView.collectionViewLayout.invalidateLayout()
配置git SSH
1、設置git的user name和email
$ git config --global user.name "username"
$ git config --global user.email "username@gmail.com"
2、生成秘鑰
$ ssh-keygen -t rsa -C "username@gmail.com"
如果不需要設置密碼,連按三個回車。最后得到了兩個文件:id_rsa
和id_rsa.pub
。
3、添加秘鑰到ssh-agent中
$ ssh-add ~/.ssh/id_rsa
4、登錄git倉庫(github或者bitbucket),添加ssh
將id_rsa.pub
文件的內容復制到對應的地方。
Apple Watch開發注意事項
1、watch沒有UIKit,對于UI的操作只能通過storyboard
進行
2、watch只支持幀動畫,我們只能通過png序列來實現動畫效果。WKInterfaceGroup
和 WKInterfaceImage
均可以實現幀動畫。
3、開發的watch應用內存被限定為80M,太多幀的動畫會不支持
4、提交應用watch也需要配置市場截圖。
5、watch分為兩個target。當新建一個Target為WatchDemo,xcode會自動生成一個WatchDemo Extension。前者負責UI,后者負責邏輯。引用cocoapods可以這么寫:
target 'WatchDemo Extension' do
platform :watchos, '3.0'
use_frameworks!
pod 'Alamofire'
end
6、Always Embed Swift Standard Libraries
在Build Settings里面,這兩個target,需要將WatchDemo Extension中設置為Yes,另一個設置為No
Github
Sizes
可以在一個界面,顯示各個屏幕尺寸。這樣我們就不用每個模擬器跑一遍看效果了。方便調試。
iOS-DeviceSupport
當手機升級,而xcode未升級時,我們會遇到Device Support的彈框,此時要么升級xcode,要么需要導入對應的Device Support文件。這個庫就是提供這種文件的。
InjectionIII
用于解決煩人的UI調試問題。當修改了一些UI屬性之后,在xcode中我們只能運行程序才能看到效果,如果是處理大量的UI問題,這個過程是很煩人的。好在InjectionIII幫我們解決了這個問題,一起了解一下吧!