Использование блоков Objective C

Это

decimal bankBalance = 3433.20M;

будет работать. Причина состоит в том, что плавание и десятичное число являются совсем другими типами. плавание даст Вам чрезвычайно близкое приближение номера, который Вы вводите, но десятичное число даст Вам точное число. 99% времени Вы уведомление привычки различие, и должны просто использовать плавание.

17
задан niton 17 April 2015 в 22:38
поделиться

3 ответа

Use newlines and break up your call across multiple lines.

The standard pattern used across all of Apple's APIs is that a method or function should only take one block argument and that argument should always be the last argument.

Which you have done. Good.

Now, when writing the code that uses said API, do something like:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSAllApplicationsDirectory, NSAllDomainsMask, YES);
paths = [paths collect: ^(id path) {
    ...
}];
paths = [paths collect: ^(id path) {
    ...
}];
paths = [paths select: ^(id path) {
    ...
}];

I.e. do each step of your collect/select/filter/flatten/map/whatever as a separate step. This will be no faster/slower than chained method calls.

If you do need to nest blocks in side of blocks, then do so with full indention:

paths = [paths collect: ^(id path) {
    ...
    [someArray select:^(id path) {
        ...
    }];
}];

Just like nested if statements or the like. When it gets too complex, refactor it into functions or methods, as needed.

19
ответ дан 30 November 2019 в 13:40
поделиться

I think the issue is that (contrary to what critics of Python claim ;) white space matters. In a more functional style, it seems like it would make sense to copy the style of other functional languages. The more LISP-y way to write your example could be something like:

NSArray *packagePaths = [[[NSSearchPathForDirectoriesInDomains(NSAllApplicationsDirectory, NSAllDomainsMask, YES) 
                            collect:^(id path) {
                                      return [[[NSFileManager defaultManager] 
                                                 contentsOfDirectoryAtPath:path 
                                                                     error:nil] 
                                               collect:^(id file) { 
                                                         return [path stringByAppendingPathComponent:file]; 
                                                        }
                                             ]; 
                                     }
                            ] 
                            flattenedArray
                          ] 

                          select:^(id fullPath) { 
                                   return [[NSWorkspace sharedWorkspace] isFilePackageAtPath:fullPath]; 
                                 }
                         ];

I wouldn't say this is clearer than the looped version. Like any other tool, blocks are a tool and they should be used only when they're the appropriate tool for the job. If readability suffers, I'd say it's not the best tool for the job. Blocks are, afterall, an addition to a fundamentally imperative language. If you really want the conciseness of a functional language, use a functional language.

2
ответ дан 30 November 2019 в 13:40
поделиться

Looks like you're re-inventing High-Order Messaging. Marcel Weiher has done extensive work on HOM in Objective-C, which you can find here:

http://www.metaobject.com/blog/labels/Higher%20Order%20Messaging.html

0
ответ дан 30 November 2019 в 13:40
поделиться
Другие вопросы по тегам:

Похожие вопросы: