我想知道使用POSIX调用pthread_once()
和/ sem_wait()
或dispatch_*函数会更好/更快,所以我创建了一个小测试并对结果感到惊讶(问题和结果在最后).
在测试代码中,我使用mach_absolute_time()来为调用计时.我真的不在乎这与纳秒没有完全匹配; 我正在将这些值相互比较,因此确切的时间单位无关紧要,只有间隔之间的差异.结果部分中的数字是可重复的而不是平均数; 我可以平均时间,但我不是在寻找确切的数字.
test.m(简单的控制台应用程序;易于编译):
#import <Foundation/Foundation.h>
#import <dispatch/dispatch.h>
#include <semaphore.h>
#include <pthread.h>
#include <time.h>
#include <mach/mach_time.h>
// *sigh* OSX does not have pthread_barrier (you can ignore the pthread_barrier
// code, the interesting stuff is lower)
typedef int pthread_barrierattr_t;
typedef struct
{
pthread_mutex_t mutex;
pthread_cond_t cond;
int count;
int tripCount;
} pthread_barrier_t;
int pthread_barrier_init(pthread_barrier_t *barrier, const pthread_barrierattr_t *attr, unsigned int count)
{
if(count == 0)
{
errno = EINVAL;
return -1;
}
if(pthread_mutex_init(&barrier->mutex, 0) < 0) …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用具有混合语言数据的FRC并希望有一个节索引.
似乎从文档中你应该能够覆盖FRC
- (NSString *)sectionIndexTitleForSectionName:(NSString *)sectionName
- (NSArray *)sectionIndexTitles
Run Code Online (Sandbox Code Playgroud)
然后使用UILocalizedIndexedCollation来获得本地化的索引和节.但遗憾的是,这不起作用,并不是打算使用的:(
有没有人能够使用带有UILocalizedIndexedCollation的FRC,或者我们被迫使用示例UITableView + UILocalizedIndexedCollation示例中提到的手动排序方法(示例代码包含在我工作的地方).
使用以下属性
@property (nonatomic, assign) UILocalizedIndexedCollation *collation;
@property (nonatomic, assign) NSMutableArray *collatedSections;
Run Code Online (Sandbox Code Playgroud)
和代码:
- (UILocalizedIndexedCollation *)collation
{
if(collation == nil)
{
collation = [UILocalizedIndexedCollation currentCollation];
}
return collation;
}
- (NSArray *)collatedSections
{
if(_collatedSections == nil)
{
int sectionTitlesCount = [[self.collation sectionTitles] count];
NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
collatedSections = newSectionsArray;
NSMutableArray *sectionsCArray[sectionTitlesCount];
// Set up the sections array: elements are mutable arrays that will contain the …
Run Code Online (Sandbox Code Playgroud) localization core-data nsfetchedresultscontroller ios uilocalizedcollation