Ich arbeite an einer App und versuchen, einen alten Obj-C-Code zu Swift portieren. Da es mit FileHandling zu tun hat und ich wenig oder keine Erfahrung darin habe, daran zu arbeiten, bin ich etwas verwirrt, wie es geht. Deshalb brauche ich deine Hilfe -So verwenden Sie CFSwapInt32BigToHost, CFSwapInt16BigToHost effektiv in Swift
Dies ist die Methode, auf der ich feststecke. Was es tut es liest eine Datei aus Dokument-Verzeichnis und dann Hops Offset-Zeiger, um die Daten zu lesen.
Ich möchte verstehen - 1. Wann und wie verwenden wir CFSwapInt32BigToHost und CFSwapInt16BigToHost in Swift? 2. Was ist UnsafeMutablePointer und wie können wir es mit data.bytes initialisieren?
- (NSMutableArray *)getTicketFareFrom:(NSString *)fromStation to:(NSString *)toStation{
NSMutableArray *ticketFareArray = [[NSMutableArray alloc] initWithCapacity:0];
NSString *directoryPath = @"TrainData/r/";
NSString* ticketFareFile = @"tf.dat";
NSString* filePath = [[NSBundle mainBundle] pathForResource:ticketFareFile ofType:@"" inDirectory:directoryPath];
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath: filePath];
if (fileHandle == nil)
NSLog(@"Failed to open file");
//Get lenght for station list
NSData* stationListLenData = [fileHandle readDataOfLength: 4];
int stationListLength = CFSwapInt32BigToHost(*(int*)([stationListLenData bytes]));
//Get station list string
[fileHandle seekToFileOffset: 4];
NSData* stationListData = [fileHandle readDataOfLength: stationListLength];
NSString *stationListString = [[NSString alloc] initWithData:stationListData encoding:NSUTF8StringEncoding];
_stationListArray = [[NSMutableArray alloc] initWithArray:[stationListString componentsSeparatedByString:@","] copyItems: YES];
//Get length for via list
[fileHandle seekToFileOffset: 4 + stationListLength];
NSData* viaListLenData = [fileHandle readDataOfLength: 4];
int viaListLength = CFSwapInt32BigToHost(*(int*)([viaListLenData bytes]));
//get via list string
[fileHandle seekToFileOffset: 4 + stationListLength + 4];
NSData* viaListData = [fileHandle readDataOfLength: viaListLength];
NSString *viaListString = [[NSString alloc] initWithData:viaListData encoding:NSUTF8StringEncoding];
_viaRouteArray = [[NSMutableArray alloc] initWithArray:[viaListString componentsSeparatedByString:@","] copyItems: YES];
NSUInteger fromIndex = [_stationListArray indexOfObject:fromStation];
NSUInteger toIndex = [_stationListArray indexOfObject:toStation];
//get length for Ticket fare data
[fileHandle seekToFileOffset: 4 + stationListLength + 4 + viaListLength];
NSData *ticketFareLenData = [fileHandle readDataOfLength: 4];
int faredata_len = CFSwapInt32BigToHost(*(int*)([ticketFareLenData bytes]));
//get ticket fare data
[fileHandle seekToFileOffset: 4 + stationListLength + 4 + viaListLength + 4];
NSData *ticketFareData = [fileHandle readDataOfLength: faredata_len];
[fileHandle closeFile];
for (int i = 0; i<faredata_len; i = i+18) {
unsigned char byte[2];
int offset = 0;
// 1.from(int)
// 2.to(int)
// 3.via index(int)
// 4.II(int)
// 5.II pass monthly(int)
// 6.II_pass_quarterly(int)
// 7.I(int)
// 8.I_pass_monthly(int)
// 9.I_pass_quarterly(int)
[ticketFareData getBytes:&byte range:NSMakeRange(i, 2)];
int from = CFSwapInt16BigToHost(*(int*)(byte));
offset = offset + 2;
[ticketFareData getBytes:&byte range:NSMakeRange(i + offset, 2)];
int to = CFSwapInt16BigToHost(*(int*)(byte));
//add if condition for From an To matching
if (from == fromIndex && to == toIndex) {
MITicketFare *ticketFare = [[MITicketFare alloc] init];
offset = offset + 2;
[ticketFareData getBytes:&byte range:NSMakeRange(i + offset, 2)];
int viaIndex = CFSwapInt16BigToHost(*(int*)(byte));
ticketFare.viaRoute = [_viaRouteArray objectAtIndex:viaIndex];
offset = offset + 2;
[ticketFareData getBytes:&byte range:NSMakeRange(i + offset, 2)];
int secondticketfare = CFSwapInt16BigToHost(*(int*)(byte));
ticketFare.secondticketfare = secondticketfare;
offset = offset + 2;
[ticketFareData getBytes:&byte range:NSMakeRange(i + offset, 2)];
int secondpass1mfare = CFSwapInt16BigToHost(*(int*)(byte));
ticketFare.secondpass1mfare = secondpass1mfare;
offset = offset + 2;
[ticketFareData getBytes:&byte range:NSMakeRange(i + offset, 2)];
int secondpass3mfare = CFSwapInt16BigToHost(*(int*)(byte));
ticketFare.secondpass3mfare = secondpass3mfare;
offset = offset + 2;
[ticketFareData getBytes:&byte range:NSMakeRange(i + offset, 2)];
int firstticketfare = CFSwapInt16BigToHost(*(int*)(byte));
ticketFare.firstticketfare = firstticketfare;
offset = offset + 2;
[ticketFareData getBytes:&byte range:NSMakeRange(i + offset, 2)];
int firstpass1mfare = CFSwapInt16BigToHost(*(int*)(byte));
ticketFare.firstpass1mfare = firstpass1mfare;
offset = offset + 2;
[ticketFareData getBytes:&byte range:NSMakeRange(i + offset, 2)];
int firstpass3mfare = CFSwapInt16BigToHost(*(int*)(byte));
ticketFare.firstpass3mfare = firstpass3mfare;
if (![self isTicketFareDuplicate:ticketFareArray ticketFare:ticketFare]) {
[ticketFareArray addObject:ticketFare];
}
}
}
return ticketFareArray;
}
Mein Versuch, Port die Methode -
func getTicketFareFrom(fromStation:String, toStation:String)->Array<TicketFareModel>
{
var ticketFareArray:Array<TicketFareModel> = [];
// Read data from File
let directoryPath = "TrainData/r/";
let ticketFareFile = "tf.dat";
let filePath = NSBundle.mainBundle().pathForResource(ticketFareFile, ofType: "" , inDirectory:directoryPath);
let fileHandle = NSFileHandle(forReadingAtPath: filePath!);
if (fileHandle == nil)
{
print("Failed to open file");
}
else if(fileHandle != nil)
{
//Get length for station list
let stationListLenData:NSData = fileHandle!.readDataOfLength(4);
let stationListLength:Int = Int(CFSwapInt32BigToHost((stationListLenData.uint32))); // instead of uint32 it should be .bytes
//Get station list string
fileHandle!.seekToFileOffset(4);
let stationListData = fileHandle!.readDataOfLength(stationListLength);
let stationListString = NSString(data: stationListData, encoding: NSUTF8StringEncoding);
// self.stationListArray = [String](stationListString.componentsSeparatedByString(","), copyItems: true);
self.stationListArray = (stationListString!.componentsSeparatedByString(","));
//Get length for via list
fileHandle!.seekToFileOffset(UInt64(4 + stationListLength));
let viaListLenData:NSData = fileHandle!.readDataOfLength(4);
let viaListLength:Int = Int(CFSwapInt32BigToHost((viaListLenData.uint32)));//CFSwapInt32BigToHost(*(int*)([viaListLenData bytes]));
//get via list string
fileHandle!.seekToFileOffset(UInt64(4 + stationListLength + 4));
let viaListData:NSData = fileHandle!.readDataOfLength(viaListLength);
let viaListString = NSString(data: viaListData, encoding: NSUTF8StringEncoding);
self.viaRouteArray = (viaListString!.componentsSeparatedByString(",")); //[[NSMutableArray alloc] initWithArray:[viaListString componentsSeparatedByString:@","] copyItems: YES];
let fromIndex: Int = self.stationListArray.indexOf(fromStation)!;
let toIndex: Int = stationListArray.indexOf(toStation)!;
//get length for Ticket fare data
fileHandle!.seekToFileOffset(UInt64(4 + stationListLength + 4 + viaListLength));
let ticketFareLenData:NSData = fileHandle!.readDataOfLength(4);
let faredata_len:Int = Int(CFSwapInt32BigToHost((ticketFareLenData.uint32))); //int faredata_len = CFSwapInt32BigToHost(*(int*)([ticketFareLenData bytes]));
//get ticket fare data
fileHandle!.seekToFileOffset(UInt64(4 + stationListLength + 4 + viaListLength + 4));
let ticketFareData:NSData = fileHandle!.readDataOfLength(faredata_len);
fileHandle!.closeFile();
/* */
for (var i:Int = 0; i<faredata_len; i = i+18)
{
var byte: [UInt8];
var offset:Int = 0;
ticketFareData.getBytes(&byte, range: NSMakeRange(i, 2))
var from: Int = Int(CFSwapInt16BigToHost((ticketFareData.uint16)));//CFSwapInt16BigToHost(Int(&byte))
offset = offset + 2
ticketFareData.getBytes(&byte, range: NSMakeRange(i + offset, 2))
var to: Int = CFSwapInt16BigToHost(Int(byte))
if from == fromIndex && to == toIndex
{
var ticketFareModel: TicketFareModel = TicketFareModel()
offset = offset + 2
ticketFareData.getBytes(&byte, range: NSMakeRange(i + offset, 2))
var viaIndex: Int = CFSwapInt16BigToHost(Int(byte))
ticketFareModel.viaRoute = viaRouteArray[viaIndex]
offset = offset + 2
ticketFareData.getBytes(&byte, range: NSMakeRange(i + offset, 2))
var secondticketfare: Int = CFSwapInt16BigToHost(Int(byte))
ticketFareModel.secondticketfare = secondticketfare
offset = offset + 2
ticketFareData.getBytes(&byte, range: NSMakeRange(i + offset, 2))
var secondpass1mfare: Int = CFSwapInt16BigToHost(Int(byte))
ticketFareModel.secondpass1mfare = secondpass1mfare
offset = offset + 2
ticketFareData.getBytes(&byte, range: NSMakeRange(i + offset, 2))
var secondpass3mfare: Int = CFSwapInt16BigToHost(Int(byte))
ticketFareModel.secondpass3mfare = secondpass3mfare
offset = offset + 2
ticketFareData.getBytes(&byte, range: NSMakeRange(i + offset, 2))
var firstticketfare: Int = CFSwapInt16BigToHost(Int(byte))
ticketFareModel.firstticketfare = firstticketfare
offset = offset + 2
ticketFareData.getBytes(&byte, range: NSMakeRange(i + offset, 2))
var firstpass1mfare: Int = CFSwapInt16BigToHost(Int(byte))
ticketFareModel.firstpass1mfare = firstpass1mfare
offset = offset + 2
ticketFareData.getBytes(&byte, range: NSMakeRange(i + offset, 2))
var firstpass3mfare: Int = CFSwapInt16BigToHost(Int(byte))
ticketFareModel.firstpass3mfare = firstpass3mfare
if !self.isTicketFareDuplicate(ticketFareArray, ticketFare: ticketFareModel)
{
ticketFareArray.append(ticketFareModel)
}
}
}
//*/
}
return ticketFareArray;
}
Ich bin in der for-Schleife stecken und ich weiß wirklich nicht, ob ich das Zeug richtig gemacht haben. Ich habe viele Fragen und Antworten zu Stackoverflow gelesen, aber nichts hat sich als nützlich für mein Verständnis erwiesen.
Kann mir bitte jemand sagen, wie soll ich es tun?
Danke.