简体   繁体   English

为什么在设备上而不在模拟器上运行时会破坏数据?

[英]Why a data will be destroyed when running on device but not on simulator?

When running my program on a iOS simulator I didn't have a problem. 在iOS模拟器上运行程序时,我没有遇到任何问题。 But when running on my iPhone5c, I have a problem. 但是在我的iPhone5c上运行时,我遇到了问题。 The problem is data will be destroyed when the data was loaded. 问题是数据在加载时将被破坏。 Here is my program source code. 这是我的程序源代码。 Where my code is wrong? 我的代码哪里有错?

overview of my program: 我的程序概述:

1.load data from "sample.txt"
2.log the data

AppDelegate.h: AppDelegate.h:

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (assign) unsigned char* bytePtr;
@property (strong, nonatomic) NSData* data;
@end

AppDelegate.m: AppDelegate.m:

#import "AppDelegate.h"

@implementation AppDelegate

- (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 load];

    return YES;
}

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

sample.txt: sample.txt的:

abcdefghijklmnopqrstuvwxyz

output: 输出:

abcdefghijklmnopqrstuvwxyz
roj

expected output: 预期输出:

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

You're accessing data as though it is a NULL-terminated cstring (using %s ). 您正在访问数据,就好像它是一个以NULL结尾的cstring(使用%s )。 Unless your file ends in a \\0 (which is doesn't appear to), your NSLog will just keep reading data until it finds one. 除非您的文件以\\ 0结尾(这似乎没有出现),否则NSLog将一直读取数据,直到找到一个为止。

If you want to read a string from a file, using stringWithContentsOfURL:encoding:error: . 如果要从文件读取字符串,请使用stringWithContentsOfURL:encoding:error:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Simulator中运行时的条件编译,而不是在设备上运行 - Conditional compile when running in Simulator as opposed to on a device 在模拟器上运行时获取设备型号名称? - Get the Device model name when running on Simulator? 在iOS设备上运行但不在模拟器中运行时,SIGABRT错误 - SIGABRT Error when running on an iOS device but not in simulator 为什么MagicalRecord在模拟器中保存数据,而不在设备中保存数据? - Why MagicalRecord is saving data in simulator, but not in the device? HTMLKit 在模拟器上运行但不在设备上 - HTMLKit running on simulator but not on device 在 Xcode 中,为什么根据模拟器中运行的设备有更多/更少的视图? - In Xcode, why are there more/less views depending on what device is running in the simulator? Swift Admob控制台显示模拟器设备ID,但在iPhone上运行时却没有? - Swift Admob console displays simulator device ID but not when running on iPhone? 错误:无法获得默认输入设备......在模拟器上运行时 - Error: couldn't get default input device… when running on simulator os_signpost在模拟器上工作,但在设备上运行时不工作 - os_signpost works on simulator but not when running on device -lxml2找不到库,仅当在设备而不是模拟器上运行时 - library not found for -lxml2 Only when running on Device, not Simulator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM