我在我的程序中创建堆栈类,我希望存储NSString值。 这是Stack类:如何在堆栈中存储NSString
@interface Stack : NSObject
- (void)push:(id)obj;
- (id)pop;
- (BOOL)isEmpty;
@end
@implementation Stack
{
NSMutableArray *stack;
}
- (id)init
{
self = [super init];
if(self!= nil){
stack = [[NSMutableArray alloc] init];
}
return self;
}
- (void)push:(id)obj
{
[stack addObject:obj];
}
- (id)pop
{
id lastobj = [stack lastObject];
[stack removeLastObject];
return lastobj;
}
- (BOOL)isEmpty
{
return stack.count == 0;
}
@end
我也有另外一个类名:TableViewController 我想,当点击在TableViewController店细胞的ID从URL
接收小区,这是我的代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// I want that xCode value with xCode2 value push in stack
NSLog(@"Row in Tab : %d",indexPath.row);
if ([Folder containsObject:[All objectAtIndex:indexPath.row]]) {
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.1.%d/mamal/filemanager.php?dir=%@&folder=%d&id",IP,xCode,indexPath.row]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLResponse *response = nil;
NSError *err = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *responseString = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
xCode2 = responseString; //this is new cell's id.I want to push this value in stack
NSLog(@"xcode : %@", xCode2);
[self performSegueWithIdentifier:@"segue4" sender:self];
}
else
{
[self performSegueWithIdentifier:@"segue3" sender:self];
}
}
最上面的代码我想点击单元格中的单元格推入堆栈中的两个值(xCode & xCode2)但我不知道要使用堆栈。
您是否在使用ARC? – dreamlax
你在哪里实现了'addObject:'etc?我想你想从'NSMutableArray'而不是'NSObject'继承。然后从'init'方法中删除'stack = ...'语句。 –
是我使用ARC – janatan