我在Swift中编写了一个String扩展函数,它接受一个Protocol类型。它根据参数运行一些检查,如果基础对象不符合要求,则抛出错误:
func satisfiesComplexityRequirements(setForthBy policy: SCPasswordComplexity) throws
{
guard isAtLeastThisLong(policy.minLength()) else
{
throw SCPolicyPasswordComplexityError.TooShort
}
let maxLen = policy.maxLength()
if maxLen > 0
{
guard isNoLongerThan(maxLen) else
{
throw SCPolicyPasswordComplexityError.TooLong
}
}
guard doesNotExceedThisNumberOfRepeatedCharacters(policy.maxRepeats()) else
{
throw SCPolicyPasswordComplexityError.TooManyRepeatingCharacters
}
}
使用(Swift)优化级别' Fast'运行此代码显示SCPasswordComplexity
对象未被保留,但它确实被释放。保留计数中的净损失导致对象被解除分配(过早地),导致最终崩溃:
Zombies Instrument catching reference to prematurely released object
为什么SCPasswordComplexity
对象不会保留在函数内?该功能清楚地引用了它。我还注意到,应用程序在运行第一个throw
语句时不会崩溃,但在运行其他两个throw
语句时崩溃。