코코아 터치에서 이진 파일을 구문 분석하는 가장 간단한 방법은 무엇입니까?
-
03-07-2019 - |
문제
32 비트 int 값을 포함하는 이진 파일 (Java로 생성)이 있다고 가정 해 봅시다. 현재 파싱을 위해 다음과 같은 Objective-C 코드를 사용하고 있습니다.
NSString *path = [[NSBundle mainBundle] pathForResource:@"foo" ofType:@"dat"];
NSFileHandle *file = [NSFileHandle fileHandleForReadingAtPath:path];
unsigned long intValue;
memcpy(&intValue, [[file readDataOfLength:4] bytes], 4);
intValue = NSSwapBigLongToHost(intValue);
[file closeFile];
첫 번째 질문은 Java의 DatainputStream에 가까운 것을 찾지 못했기 때문에 iPhone에서 일을하는 일반적인 방법인지 묻는 것입니다.
두 번째 질문은 Memcpy와의 코드 라인과 관련이 있습니다. 나는 다음과 같은 부분 (더 우아하고 덜 "낮은 수준")이 대신 작동하지 않는 이유를 의심하지 않습니다.
[[file readDataOfLength:4] getbytes:&intValue];
빌드에 대한 경고를 받고 있습니다.
'NSData' may not respond to '-getbytes:'
실행시 :
'NSInvalidArgumentException', reason: '*** -[NSConcreteData getbytes:]: unrecognized selector sent to instance
해결책
마지막 질문은 getBytes (대문자 b)를 사용하십시오.
제휴하지 않습니다 StackOverflow