我希望没有人告诉我RTFM,因为我在这里和Apple Developer上一直在努力解决这个问题并进行大量搜索.
我在此声明中收到EXC-BAD-ACCESS错误:
var thisPredicate = NSPredicate(format: "(sectionNumber == %@"), thisSection)
Run Code Online (Sandbox Code Playgroud)
thisSection的Int值为1,当我将鼠标悬停在其上时显示值1.但是在调试区域我看到了这个:
thisPredicate = (_ContiguousArrayStorage ...)
Run Code Online (Sandbox Code Playgroud)
使用String的另一个谓词显示为ObjectiveC.NSObject为什么会发生这种情况?
我在使用Swift进行字符串插值时检测到内存泄漏.使用"泄漏"工具,它将泄漏的对象显示为"Malloc 32字节",但没有负责的库或框架.这似乎是由字符串插值中的选项使用引起的.
class MySwiftObject
{
let boundHost:String?
let port:UInt16
init(boundHost: String?, port: UInt16)
{
if boundHost {
self.boundHost = boundHost!
}
self.port = port
// leaks
println("Server created with host: \(self.boundHost) and port: \(self.port).")
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我通过附加片段简单地构建String来替换字符串插值,则不会发生内存泄漏.
// does not leak
var message = "Server created with host: "
if self.boundHost
{
message += self.boundHost!
}
else
{
message += "*"
}
message += " and port: \(self.port)"
println(message)
Run Code Online (Sandbox Code Playgroud)
上面有什么我做错了,或者只是一个Swift bug?
当专门处理非可选String值时,字符串插值和字符串连接之间有什么区别?
struct MyModel {
let value1: String
let value2: String
var displayNameByConcatenation: String {
return value1 + "-" + value2
}
var displayNameByInterpolation: String {
return "\(value1)-\(value2)"
}
}
Run Code Online (Sandbox Code Playgroud)
displayNameByConcatenation和displayNameByInterpolation不同的情况?就像长 unicode 字符串一样?+或插值的行为以使它们在上面的示例中有所不同?请注意,从这个问题中我们了解到字符串插值将使用descriptionCustomStringConvertible 的 。但是String串联(运算符+)也调用吗description?
string string-concatenation string-interpolation swift swift3