当我们向下面这样执行完一次数据可查询时,要记得将数据库关闭,否则,如果此时想往同一数据库写东西的话会因为数据正在锁定收到这样的提示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做选择时间的控件时,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 复制代码默认的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 复制代码首先导入CoreSpotlight和MobileCoreServices框架,然后加入以下代码:
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 g编译markdown文件时莫名报错:
TypeError: Cannot set property 'lastIndex' of undefined 复制代码解决方案是在_config.yml中将auto_detect设为false