ios - 按下按钮后记录时数据 (char*) 出现损坏

标签 ios objective-c iphone pointers

我是 iPhone 编程的新手。 问题是当我按下一个按钮时,数据将被破坏。 我找不到我的代码哪里错了以及为什么。请帮助我。

这是我的程序的概述。

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~

示例.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

谢谢。

最佳答案

问题是您没有保留 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 对象并存储它。

关于ios - 按下按钮后记录时数据 (char*) 出现损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26082310/

相关文章:

ios - UIAlertController 因在 backgroundThread Objective-C 中出现而崩溃

ios - 弹出到 Root View 后推送 UIViewController

ios - 重复符号错误 : SBJsonParser. o?

ios - 预期的标识符或 '(' xcode 错误来了

iphone - 应用程序空闲超时 - 想要在特定的空闲间隔内实现自动锁定

ios - Firebase iOS 推送通知在首次应用启动时未收到

ios - UIView 与 uiindicator sethidden = No 在 sethidden = YES 后不起作用

iphone - 我如何在 UIView 手势下检测 UIButton 的 TouchUp 事件

iphone - Tesseract更改语言iOS

iphone - 使用自签名 SSL 证书连接到 HTTPS