我是编程和学习swift的新手,我正在学习swift 2的教程并使用swift 3,因此我跟随时会遇到一些问题,这是一个我已经适当坚持的问题.
我有一个名称表,我正在为它们制作一个滑动和删除功能,它将它们从名称变量中删除,这是一个数组.我在xcode中选择了与教程最相似的函数并将它们填满,但是当我单击删除按钮时,我的应用程序随机崩溃.这是删除按钮的代码...
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deleteAction = UITableViewRowAction(style: .destructive, title: "Delete") { (rowAction: UITableViewRowAction, indexPath: IndexPath) -> Void in
print("Deleted")
self.catNames.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
self.tableView.reloadData()
}
Run Code Online (Sandbox Code Playgroud) 我创建了2个核心数据实体,然后从编辑器菜单为它们创建了NSManagedObject子类.
但是,当我运行我的应用程序时,由于某种原因,我在所有文件的每一行都会出错.
这是一个例子,这两个实体创建的文件的错误是相同的.
文件代码是自动生成的,所以我可以在这里使用它,但不确定它的用途
import Foundation
import CoreData
extension UserExercise {
@nonobjc public class func fetchRequest() -> NSFetchRequest<UserExercise> {
return NSFetchRequest<UserExercise>(entityName: "UserExercise");
}
@NSManaged public var id: Int16
@NSManaged public var name: String?
@NSManaged public var reps: Int16
@NSManaged public var sets: Int16
@NSManaged public var weight: Int16
@NSManaged public var relationship: NSSet?
}
// MARK: Generated accessors for relationship
extension UserExercise {
@objc(addRelationshipObject:)
@NSManaged public func addToRelationship(_ value: UserRoutine)
@objc(removeRelationshipObject:)
@NSManaged public func removeFromRelationship(_ value: UserRoutine)
@objc(addRelationship:)
@NSManaged public …Run Code Online (Sandbox Code Playgroud) 我正在进行扩展,将html转换为属性字符串,代码为
extension String {
var htmlToAttributedString: NSAttributedString? {
guard let data = data(using: .utf8) else { return nil }
do {
return try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch let error as NSError {
print(error.localizedDescription)
return nil
}
}
var html2String: String {
return htmlToAttributedString?.string ?? ""
}
Run Code Online (Sandbox Code Playgroud)
我收到以下3个相同的错误
使用未解析的标识符'NSDocumentTypeDocumentAttribute'
使用未解析的标识符'NSHTMLTextDocumentType'
使用未解析的标识符'NSCharacterEncodingDocumentAttribute'
我假设我犯了一个错误的语法导致3相同的错误但我无法看到扩展需要什么?
谢谢
我有一个具有Int16属性的实体。
我想保存var userRepsCount = Int()以下步进方法设置的内容:
@IBAction func userRepsStepper(_ sender: UIStepper) {
userExerciseRepsCounter.text = Int(sender.value).description
self.userRepsCount = Int(sender.value)
}
Run Code Online (Sandbox Code Playgroud)
我想将此添加到属性并正在使用userExercise.reps = userRepsCount,但是出现错误
无法将类型“ Int”的值分配给类型“ Int16”
我的印象是Int16可以毫无问题地存储int这样的int?我在这里想念什么?
我想搜索名称键的练习字典,然后在表格视图中显示过滤结果.我正在使用这个功能
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
let filtered = exercises.filter { $0["name"] == searchText }
print(filtered)
if(filtered.count == 0){
searchActive = false;
} else {
searchActive = true;
}
self.exercisesTableView.reloadData()
}
Run Code Online (Sandbox Code Playgroud)
变量:
var exercises = [Exercise]()
var filtered: [NSMutableArray] = []
var searchActive: Bool = false
Run Code Online (Sandbox Code Playgroud)
在搜索功能中,我收到错误
Type'Exercise'没有下标成员
然后我有一个问题,结果是一个NSMutableArray,所以我不能将结果名称设置为单元格文本来显示
无法将'NSMutableArray'类型的值转换为强制类型'String'
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
if (searchActive){
cell.textLabel?.text = filtered[indexPath.row] as String
} else { …Run Code Online (Sandbox Code Playgroud)