Pages

September 6, 2013

Get a list of items from a file (eg: MAC address)

I often save a set of items in a txt file so then I can "load" them directly from that file.
For example, this is a function which purpose is read MAC addresses from a given file. Of course you can adapt it to your needs.


/** Returns all the MAC addresses written in a file.

 This function treats every line as a MAC address.

 @param filePath The path of the file to search through.
 @return An array containing all the MAC addresses found in the file.
 @see macAddressWithColonsFromString:
 @see isValidMacAddress:

 */
+ (NSArray *)getMacAddressesFromFileAtPath:(NSString *)filePath {

    if (filePath) {
        NSString *str = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
        NSArray *arr = [[NSArray alloc] initWithArray:[str componentsSeparatedByString:@"\n"]];
        return arr;
    } else {
        return nil;
    }
}



Download the ready-for-use source file (.m) of Get a list of items from a file (eg: MAC address)

No comments:

Post a Comment