iPhone,目标c重新分配并返回方法指针
大家好,最近我问了很多关于 iPhone 内存管理的问题。幸运的是,事情正在变得越来越清晰。但当事情变得更加复杂时,我仍然很挣扎:那么在内存管理方面这是否有问题?我的问题和建议都在评论里...
//I get a text from a textfield
NSString *text = [[NSString alloc]initWithString:txtField.text];
NSMutableString *newText = [self replaceDynamicRegex:text];
[text release];
...
//The method replaces regex it finds in the text. The regex part is just pseudo code
//and I just interested in memory management
-(NSMutableString*)replaceDynamicRegex:(NSString*)txt{
NSString *currentTag = [NSString stringWithString:@"dynamiclyCreatedTag"];
//As long as we find a particuar regex (just pseuo code here) we replace it
while (currentTag != NULL) {
if([html stringByMatching:openingTag] == NULL){
break;
}
//regular expression
currentTag = [NSString stringWithString:[html stringByMatching:theRegex]];
//Get rid of the useless part of the currentTag pseudo code
NSString *uselessTagPart = @"uselessRegex";
//Reassignment of the pointer currentTag --> ok to do this? cause I did not alloc]init]?
//and instead used stringWithString wich then gets autoreleased
currentTag = [currentTag stringByReplacingOccurrencesOfRegex:uselessTagPart withString:@""];
//Reassignment of the pointer html --> Ok to do this? cause it is just a pointer and the
//object is being released after the method call (further up)
html = (NSMutableString*)[html stringByReplacingOccurrencesOfRegex:currentTag withString:replacementTag];
}
//Do I need to autorelease this?
return html;
}
Hey guys, lately I have been asking quite a few questions about memory management on the iPhone. Fortunately things are getting clearer. But I still struggle when it gets more complex: So is there something wrong with this in terms of memory mangement? My question and suggestions are in the comments...
//I get a text from a textfield
NSString *text = [[NSString alloc]initWithString:txtField.text];
NSMutableString *newText = [self replaceDynamicRegex:text];
[text release];
...
//The method replaces regex it finds in the text. The regex part is just pseudo code
//and I just interested in memory management
-(NSMutableString*)replaceDynamicRegex:(NSString*)txt{
NSString *currentTag = [NSString stringWithString:@"dynamiclyCreatedTag"];
//As long as we find a particuar regex (just pseuo code here) we replace it
while (currentTag != NULL) {
if([html stringByMatching:openingTag] == NULL){
break;
}
//regular expression
currentTag = [NSString stringWithString:[html stringByMatching:theRegex]];
//Get rid of the useless part of the currentTag pseudo code
NSString *uselessTagPart = @"uselessRegex";
//Reassignment of the pointer currentTag --> ok to do this? cause I did not alloc]init]?
//and instead used stringWithString wich then gets autoreleased
currentTag = [currentTag stringByReplacingOccurrencesOfRegex:uselessTagPart withString:@""];
//Reassignment of the pointer html --> Ok to do this? cause it is just a pointer and the
//object is being released after the method call (further up)
html = (NSMutableString*)[html stringByReplacingOccurrencesOfRegex:currentTag withString:replacementTag];
}
//Do I need to autorelease this?
return html;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码在内存管理方面看起来是正确的。请记住,如果您没有使用
alloc
、new
、retain
或copy
调用方法方法名,不用担心释放。一个小问题是,您的前 3 行代码是多余且低效的。您通常不应该使用
initWithString
- 在处理不可变对象时,copy
通常是更好的选择,因为在幕后,copy
方法可以被(更便宜的)retain
方法取代。在您的情况下,您甚至不需要使用copy
--[self replacementDynamicRegex: txtField.text]
将获得相同的结果。同样,您可以简单地使用[html stringByMatching:theRegex]
(因为该方法返回一个新字符串),而不是[NSString stringWithString:[html stringByMatching:theRegex]]
。另一个注释 -
html = (NSMutableString*)[html stringByReplacingOccurrencesOfRegex:currentTag withString:replacementTag]
不正确。stringByReplacingOccurrencesOfRegex:
返回一个NSString
,它无法转换为NSMutableString
(稍后当您发送一个将方法转变为字符串)。相反,使用[[html stringByReplacingOccurrencesOfRegex:currentTag withString:replacementTag] mutableCopy]
Your code looks correct memory-management-wise. Just remember, if you don't have call a method with
alloc
,new
,retain
, orcopy
in the method name, you don't have to worry about releasing.One small point--your first 3 lines of code are redundant and inefficient. You shouldn't usually use
initWithString
--copy
is usually a better choice when dealing with immutable objects, since behind the scenes acopy
method can be replaced by a (less expensive)retain
method. In your case, you don't even need to usecopy
--[self replaceDynamicRegex: txtField.text]
will have the same result. Likewise, instead of[NSString stringWithString:[html stringByMatching:theRegex]]
, you can use simply use[html stringByMatching:theRegex]
(since that method returns a new string).Another note--
html = (NSMutableString*)[html stringByReplacingOccurrencesOfRegex:currentTag withString:replacementTag]
is incorrect.stringByReplacingOccurrencesOfRegex:
returns anNSString
, which can't be cast to anNSMutableString
(you'll likely get a crash later on when you send a mutating method to the string). Instead, use[[html stringByReplacingOccurrencesOfRegex:currentTag withString:replacementTag] mutableCopy]
通常,当您看到名为
xWithY
的方法时,您可以假设该字符串将为autorelease
-d。因此,您可能不需要需要
自动释放
从-stringByReplacingOccurrencesOfRegex:withString:
。对我来说,你的其余代码看起来不错。
Generally, when you see a method named
xWithY
, you can assume the string will beautorelease
-d.Therefore, you probably do not need to
autorelease
the value returned from-stringByReplacingOccurrencesOfRegex:withString:
.The rest of your code looks okay, to me.