所以我拥有的是一个我希望能够在另一个类中访问的 NSString。在我的 RootViewController.h 我有:
@interface RootViewController : UITableViewController
+(NSMutableString*)MY_STR;
@property (nonatomic, readwrite, retain) NSString *MY_STR;
@end
在我的 RootViewController.m 中:
static NSString* MY_STR;
@synthesize MY_STR;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//The NSDictionary and NSArray items (listOfItems, etc.) are called at top so don't worry about them
NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"MovieTitles"];
MY_STR = [array objectAtIndex:indexPath.row];
}
+(NSString*)MY_STR{
return MY_STR;
}
- (void)dealloc {
[MY_STR release];
[super dealloc];
}
现在在我的 NewViewController 类中,我想写入 NSString MY_STR 所以在我的 .m 中我有:
#import "RootViewController.h"
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"MovieTitles"];
[RootViewController MY_STR] = [array objectAtIndex:indexPath.row];
}
但在这一行:
[RootViewController MY_STR] = [array objectAtIndex:indexPath.row];
我收到此错误:“分配给 'readonly' 不允许返回 Objective-c 消息的结果”
任何帮助都会很棒,谢谢!