iOS開發月報#2|201808

數據庫用完要close

當我們向下面這樣執行完一次數據可查詢時,要記得將數據庫關閉,否則,如果此時想往同一數據庫寫東西的話會因爲數據正在鎖定收到這樣的提示database is locked

//獲取下載完成的文件信息
func isExistdWith(_ id: String) -> Bool{
guard db.open() else {
return false
}
do {
let resultSet = try db.executeQuery("select * from tableName where id = ?", values: [id])
if resultSet.next() {

let isCompleted = resultSet.bool(forColumn: self.isCompleted)
db.close()//return之前要close數據庫
return isCompleted
}
} catch {

}
db.close()//return之前要close數據庫
return false
}
複製代碼

UIDatePicker的時間格式

當我們用UIDatePicker做選擇時間的控件時,DatePicker會根據手機時間的設置自動選擇是12小時制還是24小時制,如果我們需要強制控制DatePicker是顯示12小時制還是24小時制可以這麼做:

datePicker.datePickerMode = .time
datePicker.locale = Locale.init(identifier: "en_GB")//for 24 Hrs
datePicker.locale = Locale.init(identifier: "en_US")//for 12 Hrs
複製代碼

iOS skill map

變量對外只讀,對內可讀寫

struct Person {
private(set) var name : String!
}
複製代碼

設置UITableViewCell分割線對齊

默認的cell分割線都是偏向右邊多一些的,如果我們想讓分割線對齊的話,正確的做法是:

tableView.separatorInset = UIEdgeInsets.init(top: 0, left: 40, bottom: 0, right: 40)
複製代碼

設置左右邊距都是40 但是使用這種方法會帶來一個問題就是默認的textLabel會跟着右移。爲了保持label的居中我們可以再加一句:

tableView.separatorInset = UIEdgeInsets.init(top: 0, left: 40, bottom: 0, right: 40)
tableView.layoutMargins = UIEdgeInsets.init(top: 0, left: 40, bottom: 0, right: 40)
複製代碼

富文本顯示圖片元素

如果我們需要文字中插入圖片元素時,可以使用富文本處理:

let attch = NSTextAttachment()
attch.image = UIImage.(named:"logo")
attch.bounds = CGRect.init(x: 0, y: 0, width: 18, height: 18)
let imageAttribute = NSAttributedString(attachment: attch)
titleLabel.attributedText = attributed
複製代碼

添加spotlight搜索索引

首先導入CoreSpotlightMobileCoreServices框架,然後加入以下代碼:

var searchableItems = [CSSearchableItem]()
//索引項
let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeData as String)
//title
attributeSet.title = "Item Title"
//desription
attributeSet.contentDescription = "match.description"
//thumb
attributeSet.thumbnailData = try? Data.init(contentsOf: URL(string: url)!)
//keywords
attributeSet.keywords = ["Love", "Peace"]
let searchableItem = CSSearchableItem(uniqueIdentifier: "app_keywords", domainIdentifier: "com.company.app", attributeSet: attributeSet)
searchableItems.append(searchableItem)
//建立索引            
CSSearchableIndex.default().indexSearchableItems(searchableItems) { (error) -> Void in
if error != nil {
print(error?.localizedDescription ?? "Error")
}
}
複製代碼

Hexo編譯問題

在執行hexo g編譯markdown文件時莫名報錯:

TypeError: Cannot set property 'lastIndex' of undefined
複製代碼

解決方案是在_config.yml中將auto_detect設爲false

Podfile用法

# 下面兩行是指明依賴庫的來源地址
source 'https://github.com/CocoaPods/Specs.git'
source 'https://github.com/Artsy/Specs.git'

# 說明平臺是ios,版本是8.0
platform :ios, '8.0'

# 忽略引入庫的所有警告(強迫症者的福音啊)
inhibit_all_warnings!

# 生成的是framework而不是靜態庫
use_frameworks!

# 針對MyApp target引入AFNetworking
# 針對MyAppTests target引入OCMock,
target 'MyApp' do 
pod 'AFNetworking', '~> 3.0' 
target 'MyAppTests' do
inherit! :search_paths 
pod 'OCMock', '~> 2.0.1' 
end
pod 'JSONKit', :podspec => 'https://example.com/JSONKit.podspec'
# 引入內部庫
pod 'ABTest', :git => 'https://bitbucket.org/sealcn/remoteabtest.git'
pod 'ABTest', :git => 'https://bitbucket.org/sealcn/remoteabtest.git', :tag=> '0.0.6'
pod 'ABTest', :git => 'https://bitbucket.org/sealcn/remoteabtest.git', :commit=> '082f8319af'
# 編譯配置,指定僅在Debug模式下啓用
pod 'Reveal-SDK', :configurations => ['Debug']
# 使用本地文件
pod 'AFNetworking', :path => '~/Documents/AFNetworking'
# 指定版本號0.1.3到0.2,不包括0.2
pod 'CHIPageControl', '~> 0.1.3'
# 僅安裝QueryKit下的Attribute和QuerySet模塊
pod 'QueryKit', :subspecs => ['Attribute', 'QuerySet']


end
# 這個是cocoapods的一些配置,官網並沒有太詳細的說明,一般採取默認就好了,也就是不寫.
post_install do |installer|       
installer.pods_project.targets.each do |target| 
puts target.name 
end
end
複製代碼
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章