领域:如何在数据库字符串条目中使用换行符?

时间:2016-04-03 17:04:10

标签: ios swift2 realm

我在我的iOS应用程序中使用了一个Realm DB,并希望通过专门放置的换行符来解决长时间的问题。我在文本中插入了 \ n ,但在显示时,它们被视为文本而不是换行符。

有没有办法在存储在数据库中的String中插入新行,以便在显示String时识别?

1 个答案:

答案 0 :(得分:0)

Realm stores whatever you put in there, so if you're storing Swift.Strings, it'll store them exactly byte-for-byte using UTF8 encoding. So really this is a question of how to properly escape newlines in a Swift String.

print("a\nb")
// Prints:
// a
// b

Same thing goes for Realm:

import RealmSwift

class MyModel: Object {
    dynamic var stringProperty = ""
}
let realm = try! Realm(configuration: Realm.Configuration(inMemoryIdentifier: "TemporaryRealm"))
try! realm.write {
    let object = MyModel()
    object.stringProperty = "a\nb"
    realm.add(object)
}
print(realm.objects(MyModel).first!.stringProperty)
// Prints:
// a
// b