按下按钮后,数据(char *)在登录后出现损坏

问题描述:

我对iphone编程很新手。 问题是当我按下按钮时,数据将被销毁。 我找不到我的代码错在哪里以及为什么。请帮帮我。按下按钮后,数据(char *)在登录后出现损坏

这是我的程序概述。

1. load text data from "sample.txt" 
2. log the data 
3. When I push a button, it logs the data again. 

AppDelegate.h

#import <UIKit/UIKit.h> 

@interface AppDelegate : UIResponder <UIApplicationDelegate> 

@property (strong, nonatomic) UIWindow *window; 
@property (strong, nonatomic) UIButton *myButton1; 
@property (assign) unsigned char* bytePtr; 

@end 

AppDelegate.m:

~snip~ 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 
    self.window.backgroundColor = [UIColor whiteColor]; 
    [self.window makeKeyAndVisible]; 

    self.myButton1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [self.myButton1 setFrame:CGRectMake(0, 0, 100 ,100)]; 
    [self.myButton1 addTarget:self action:@selector(button1DidPushed) forControlEvents:UIControlEventTouchUpInside]; 
    [self.myButton1 setTitle:@"push" forState:UIControlStateNormal]; 

    [self.window addSubview:self.myButton1]; 
    [self load]; 

    return YES; 
} 


- (void) load 
{ 
    NSString* path = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"txt"]; 
    NSData* data = [NSData dataWithContentsOfFile:path]; 
    self.bytePtr = (unsigned char *)[data bytes]; 
    NSLog(@"%s", self.bytePtr); 
} 


- (void)button1DidPushed 
{ 
    UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"Enter something..." message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 
    alerView.alertViewStyle = UIAlertViewStylePlainTextInput; 
    [alerView show]; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex]; 
    if([title isEqualToString:@"OK"]) 
    { 

    } 
    NSLog(@"%s", self.bytePtr); 
} 

~snip~ 

sample.txt的:

abcdefghijklmnopqrstuvwxyz 

输出:

abcdefghijklmnopqrstuvwxyz 
<----- When I push the button the mark similar to "?" will appear on output window.(I couldn't show it here(It's probabily ascii code 2(STX))) That is why I think the data is destroyed. 

环境:

xcode 6.0.1 

感谢。

+2

如果你希望你的'bytePtr'存储数据,它不会。当数据超出范围时,该指针的值将毫无意义。你应该阅读关于指针/通用C编程。 – sapi 2014-09-28 06:38:37

的问题是,你是不是保留NSData对象:

- (void) load 
{ 
    NSString* path = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"txt"]; 
    NSData* data = [NSData dataWithContentsOfFile:path]; // here 
    self.bytePtr = (unsigned char *)[data bytes]; 
    NSLog(@"%s", self.bytePtr); 
} 

一旦方法返回NSData对象将被销毁,因此缓冲指向self.bytePtr不再有效。

要解决此问题,请将self.bytePtr更改为NSData对象并将其存储。

+0

啊我明白了。谢谢。 – 2014-09-28 09:09:56

+0

@keimina忘了提及,如果这些数据是由字符组成的,你应该使用'NSString'而不是'NSData'。因此,使用所需的编码将'NSData'转换为'NSString',并将其存储起来。 – Droppy 2014-09-28 09:11:48

首先,为了接收您的alertView:clickedButtonAtIndex:方法调用,您需要将您的UIAlertView代理设置为self。

[alertView setDelegate:self] // make sure you set UIAlertViewDelegate protocol on the method owner 

否则应该使用使用initWithData:encoding:NSString对象初始化。