English 中文(简体)
“将目标-C法典转换到快速:从书目数据中提取财产”
原标题:"Translating Objective-C Code to Swift: Struggling with Properties Extraction from Bookmark Data"Help me to translate this code

I m目前致力于将目标C中写成的一部法典转换成迅速,我碰到了一条路障。 给我带来麻烦的部分涉及从书记数据中提取财产。

NSMutableArray *extractItems(NSString *path)
{
    NSDictionary *data = [NSDictionary dictionaryWithContentsOfFile:path];
    NSMutableArray* items = [[NSMutableArray alloc] init];
    NSData* bookmark = nil;
    NSDictionary* properties = nil;
    
    // Extract items
    for(id object in data[@"$objects"])
    {
        // Reset bookmark
        bookmark = nil;
        
        // Extract bookmark data
        if([object isKindOfClass:[NSData class]] == YES)
        {
            bookmark = object;
        }
        
        // Extract bookmark from dictionary
        if([object isKindOfClass:[NSDictionary class]] == YES)
        {
            bookmark = [object objectForKey:@"NS.data"];
        }
        
        // Skip if no bookmark
        if(bookmark == nil)
        {
            continue;
        }
        
        // Extract properties
        properties = [NSURL resourceValuesForKeys:@[@"AllPropertiesKey"] fromBookmarkData:bookmark][@"AllPropertiesKey"];
        if(properties == nil)
        {
            // Skip this item
            continue;
        }
        
        // Create item
        Item *item = [[Item alloc] init];
        item.path = path;
        item.executable = properties[@"_NSURLPathKey"];
        item.type = [NSNumber numberWithInt:ItemType];
        item.user = extractUserFromPath(path);
        item.content = nil;
        
        // Use name from bundle or NSURLNameKey
        item.name = [NSBundle bundleWithPath:item.executable].infoDictionary[@"CFBundleName"];
        if(item.name.length == 0)
        {
            item.name = properties[@"NSURLNameKey"];
        }
        
        // Skip if there are issues
        if (item.name == nil || item.executable == nil)
        {
            continue;
        }
        
        // Add item
        [items addObject:item];
    }
    
    return items;
}

面临的挑战Im涉及利用资源ValuesForKeys方法开采财产,然后在其中使用NSURLBookmark AllpertiesKey dictionary。

最佳回答

内容如下:

// Extract properties
properties = [NSURL resourceValuesForKeys:@[@"AllPropertiesKey"] fromBookmarkData:bookmark][@"AllPropertiesKey"];
if(properties == nil)
{
    // Skip this item
    continue;
}

如同此:

// Extract properties
let allPropertiesKey = URLResourceKey("AllPropertiesKey")
guard let properties = NSURL.resourceValues(forKeys: [ allPropertiesKey ], fromBookmarkData: bookmark)?[allPropertiesKey] as? [URLResourceKey: Any] else {
    // Skip this item
    continue;
}

我认为,最后的投放是正确的,但确实取决于编造的<条码>“PropertiesKey”的关键回报。 根据您的法典其余部分,。 关键:任何]似乎都有可能。

如果这一切都奏效,那么,就象:

item.name = properties[@"NSURLNameKey"];

将:

item.name = properties[.nameKey] as? String // or as! but write that defensively
问题回答

暂无回答




相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...