小编Pet*_*isu的帖子

C++中的静态类

在我声明的标题中

#ifndef SOUND_CORE
#define SOUND_CORE

static SoundEngine soundEngine;

...
Run Code Online (Sandbox Code Playgroud)

但SoundEngine的构造函数被多次调用,当它被声明为全局静态时,它是如何可能的

我称之为

#include "SoundCore.h"
Run Code Online (Sandbox Code Playgroud)

并直接使用它

soundEngine.foo()
Run Code Online (Sandbox Code Playgroud)

谢谢

c++ static class

4
推荐指数
2
解决办法
212
查看次数

Cocoa如何:addObserver在引擎盖下工作?

如何addObserver:在引擎盖下工作?由于Objective C不能超载运营商......

@properties或对象引用是否隐式存储在a中NSDictionary,可以监视值访问?或者可可如何观察价值?

特别是当我们自己编写getter和setter属性时,观察者通知是如何发生的?

cocoa notifications objective-c

4
推荐指数
1
解决办法
512
查看次数

UICollectionView委托方法cellForItemAtIndexPath:未从sizeForItemAtIndexPath调用indexPath

我打电话的时候

[collectionView cellForItemAtIndexPath:indexPath:]
Run Code Online (Sandbox Code Playgroud)

从内部

[collectionView:layout:sizeForItemAtIndexPath:]
Run Code Online (Sandbox Code Playgroud)

然后不会触发委托方法.知道为什么不呢?

你可以在这里看到它.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    CustomCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"CustomCell" forIndexPath:indexPath];

    [cell configureWithData:self.data[indexPath.row]];

    return cell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    CustomCell * cell = (CustomCell *)[collectionView cellForItemAtIndexPath:indexPath];

    return [cell preferredSize];
}
Run Code Online (Sandbox Code Playgroud)

我想要做的是询问电池的首选尺寸.

我可以

CustomCell * cell = (CustomCell *)[self collectionView:collectionView cellForItemAtIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)

但随后会触发一个永无止境的循环周期

为什么它的委托方法不应该被调用呢?

cocoa-touch objective-c ios ios6 uicollectionview

4
推荐指数
1
解决办法
5855
查看次数

NSDate以正确的方式添加一个月,如果前一个月有更多天,则剪裁

我使用此方法将月份添加到日期

- (NSDate *)sameDateByAddingMonths:(NSInteger)addMonths {

    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDateComponents * components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:self];
    [components setMonth:components.month + addMonths];

    return [calendar dateFromComponents:components];

}
Run Code Online (Sandbox Code Playgroud)

但是当前一个月在自我NSDate,有更多的日子,比它在下个月的第一天跳跃,就像

6月31日=>自我是6月31日召集这个,将日期设定为1. 8月,自7月起30天

如何做到这一点?我认为这应该表现得"正确",并在月末剪辑

objective-c nsdate nscalendar

4
推荐指数
1
解决办法
2432
查看次数

iOS和OSX代码的宏条件

我需要在我的共享库中支持iOS和OSX,它是用于调整iOS和OSX代码的最佳宏/实践

波纹管代码由于某种原因不起作用:/

- (NSString *)hostName {
    #ifdef TARGET_OS_IPHONE
        return [[UIDevice currentDevice] name];
    #elif TARGET_OS_MAC
        return [[NSHost currentHost] localizedName];
    #endif
}
Run Code Online (Sandbox Code Playgroud)

macros cocoa cocoa-touch objective-c ios

4
推荐指数
1
解决办法
2088
查看次数

在iOS上用std编写文件

我在xCode中添加了一个save.txt到我的iPhone资源,文本"..."

我写了一些字符串到文件,而不是阅读它,但当我读它,我只得到旧内容..所以没有新的写入其中

NSString* resources = [ [ NSBundle mainBundle ] resourcePath ] ;    

std::string respath( [ resources UTF8String ] ) ;

std::string fpath = respath + "/" + "save.txt" ;


std::ofstream file;

file.open(fpath.c_str(), std::ios::out );


file << "some text\n";

file.close();


std::ifstream rf;

rf.open(fpath.c_str(), std::ios::in );

char str[255];
while(rf) {
    rf.getline(str, 255);  

    if(rf) printf("%s", str);

}   
Run Code Online (Sandbox Code Playgroud)

c++ iphone stl file ios

3
推荐指数
1
解决办法
2085
查看次数

PackageMaker什么都不安装

我有一个问题,就像这里http://compgroups.net/comp.sys.mac.apps/PackageMaker-not-installing-my-app(没有提供解决方案)

简单地说,使用PackageMaker我创建一个安装程序,它应该只复制.app里面的/ Applications

一切顺利,安装程序的大小为150MB,安装开始,iam提示安装位置和管理员密码,我点击安装,进度显示和安装成功的窗口,但是当我查看应用程序时,或指定的自定义文件夹,它什么都没有

我尝试从Xcode 3和4中的PackageManager,但结果是一样的,我也尝试为每个人设置文件权限,所以他们是世界可写的,但没有成功

macos installer xcode package-managers

3
推荐指数
1
解决办法
1258
查看次数

CoreData谓词最新存储日期

我需要从coredata获取最新日期

我找到了一个方法

NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];

[fetchRequest setFetchLimit:1];
Run Code Online (Sandbox Code Playgroud)

所以按日期排序然后选择第一个

但是这不能做得更好吗?这种方法看起来像是一种蛮力

sort是nlogn,但是对max的简单搜索是n

cocoa core-data

3
推荐指数
1
解决办法
1726
查看次数

__autoreleasing错误:(NSError*__ autoreleasing*)outError

我注意到Apple函数中的这种模式会返回错误

error:(NSError *__autoreleasing *)outError
Run Code Online (Sandbox Code Playgroud)

我理解它的含义,它指向指针,用于执行结果(使用*只会改变本地复制的变量,但不会改变外部的变量)但是我关心的是

__autoreleasing

如果我把它留下怎么办?我有泄漏吗?为什么有必要?

........ ............. .........

cocoa cocoa-touch objective-c automatic-ref-counting

3
推荐指数
1
解决办法
2380
查看次数

NSURLSession delegateQueue的maxConcurrentOperationCount是如何影响任务并发的

我正在使用 AFNetworking 3.0,它使用NSURLSessionDataTask了一个 operationQueue(属性AFURLSessionManager),它用作NSURLSession

所以简单地说

AFURLSessionManager.operationQueue == NSURLSession.delegateQueue

AFNetworking 3 代码

self.operationQueue = [[NSOperationQueue alloc] init];
self.operationQueue.maxConcurrentOperationCount = 1;
self.session = [NSURLSession sessionWithConfiguration:self.sessionConfiguration delegate:self delegateQueue:self.operationQueue];
Run Code Online (Sandbox Code Playgroud)

来自苹果文档。到 NSURLSession 委托队列

用于调度委托调用和完成处理程序的操作队列。该队列不必是串行队列。如果为零,会话将创建一个串行操作队列,用于执行所有委托方法调用和完成处理程序调用。

设置self.operationQueue.maxConcurrentOperationCount = 4;意味着会有最大值。4 同时处理NSURLSessionDataTask? 并将其设置为 = 1(AFNetworking 的默认值)意味着当时只有一项任务,并且必须完成另一项任务才能开始?

或者我错了,这只会影响委托消息传递,这意味着将其设置为 4 可以在 4 个不同的线程上触发委托回调......而设置为 1 意味着回调始终在同一线程上,但是无论如何,任务都是并发处理的

multithreading cocoa-touch objective-c ios afnetworking

3
推荐指数
1
解决办法
1723
查看次数