-
Notifications
You must be signed in to change notification settings - Fork 1
/
BasicCode
130 lines (89 loc) · 3.44 KB
/
BasicCode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
///A file where miscellaneous code is pasted.
//Bullet point text
//need to convert to Swift 5 and test
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
// If the replacement text is "\n" and the
// text view is the one you want bullet points
// for
if (text == "\n") {
// If the replacement text is being added to the end of the
// text view, i.e. the new index is the length of the old
// text view's text...
if range.location == textView.text.characters.count {
// Simply add the newline and bullet point to the end
var updatedText: String = textView.text!.stringByAppendingString("\n \u{2022} ")
textView.text = updatedText
}
else {
// Get the replacement range of the UITextView
var beginning: UITextPosition = textView.beginningOfDocument
var start: UITextPosition = textView.positionFromPosition(beginning, offset: range.location)!
var end: UITextPosition = textView.positionFromPosition(start, offset: range.length)!
var textRange: UITextRange = textView.textRangeFromPosition(start, toPosition: end)!
// Insert that newline character *and* a bullet point
// at the point at which the user inputted just the
// newline character
textView.replaceRange(textRange, withText: "\n \u{2022} ")
// Update the cursor position accordingly
var cursor: NSRange = NSMakeRange(range.location + "\n \u{2022} ".length, 0)
textView.selectedRange = cursor
}
return false
}
// Else return yes
return true
}
///realm
import UIKit
import RealmSwift
class List: Object {
@objc dynamic var name = ""
@objc dynamic var description = ""
@objc dynamic var contents = ""
@objc dynamic var iconImageName = ""
//@objc dynamic var created = Date()
}
import RealmSwift
var lists: Results<Item>?
let realm = try! Realm()
class ListBrowserViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
func loadLists() {
lists = realm.objects(List.self)
tableView.reloadData()
}
func addNewList(name: String, desc: String, contents: String, iconName: String) {
do {
try realm.write { // 2
let newList = List() // 3
newList.name = name
newList.description = desc
newList.contents = contents
newList.iconImageName = iconName
realm.add(newList) // 5
lists.append(newList)
}
} catch {
print("error saving, \(error)")
}
tableView.reloadData()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return lists.count ?? 1
}
}
//MARK: - Search bar methods
extension ListBrowserViewController: UISearchBarDelegate {
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
lists = lists?.filter("title CONTAINS[cd] %@", searchBar.text!).sorted(byKeyPath: "name", ascending: true)
tableView.reloadData()
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchBar.text?.count == 0 {
loadLists()
DispatchQueue.main.async {
searchBar.resignFirstResponder()
}
}
}
}
}