取决于 Monogenerator 与否,我将使用第二种方法。覆盖“getters”和“setters”,执行从 URL 到字符串的映射
NSString *urlString = [url absoluteString];
并使用setPrimitiveValue:forKey:
及其对应物
- (void)setYourURL:(NSURL*)newURL {
[self willChangeValueForKey:@"yourURL"]; // call to notify the world you are modifying that property
NSString* yourURLAsString = // perform the conversion here...
[self setPrimitiveValue:yourURLAsString forKey:@"yourURL"];
[self didChangeValueForKey:@"yourURL"]; // call to notify the world you've modified that property
}
如果您的 URL 有空格,您也可以考虑将它们替换为stringByReplacingPercentEscapesUsingEncoding:
.