iPhone,目标c重新分配并返回方法指针

发布于 2024-09-11 11:40:44 字数 1555 浏览 0 评论 0原文

大家好,最近我问了很多关于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可是我不能没有你 2024-09-18 11:40:44

您的代码在内存管理方面看起来是正确的。请记住,如果您没有使用 allocnewretaincopy 调用方法方法名,不用担心释放。

一个小问题是,您的前 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, or copy 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 a copy method can be replaced by a (less expensive) retain method. In your case, you don't even need to use copy--[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 an NSString, which can't be cast to an NSMutableString (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]

甚是思念 2024-09-18 11:40:44

通常,当您看到名为 xWithY 的方法时,您可以假设该字符串将为 autorelease-d。

因此,您可能不需要需要自动释放-stringByReplacingOccurrencesOfRegex:withString:

对我来说,你的其余代码看起来不错。

Generally, when you see a method named xWithY, you can assume the string will be autorelease-d.

Therefore, you probably do not need to autorelease the value returned from -stringByReplacingOccurrencesOfRegex:withString:.

The rest of your code looks okay, to me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文