小编wjl*_*wjl的帖子

为什么time(time_t*)函数都返回并设置by-ref?

我一直很好奇,为什么time(time_t *)函数都返回一个time_t,并设置传入指针的时间?

返回时间的示例:

time_t myTime = time(NULL);
printf("The time is now %s", ctime(&myTime));
Run Code Online (Sandbox Code Playgroud)

将值设置为指针的示例:

time_t myTime;
time(&myTime);
printf("The time is now %s", ctime(&myTime));
Run Code Online (Sandbox Code Playgroud)

我原本以为通过写入内存而不是返回会有性能提升,但是如果它必须同时执行这两种操作,那是不是只会让它变慢?

c time libc

16
推荐指数
2
解决办法
4064
查看次数

AVCaptureDevice:Flash还是火炬?

查看AVCaptureDevice,火炬和闪光灯设置都有属性.他们似乎都有相同的方法.我知道"火炬"是英国狂热者对手电筒的说法,但这两个名字真的完全相同吗?

iphone avfoundation

13
推荐指数
1
解决办法
5336
查看次数

应该在alloc/new/init方法上使用instancetype吗?

按照铛文件,返回一个方法id是隐含已知返回instancetype时,它是一类方法开头newalloc,或实例方法开头retain,autorelease,init,或self.

为了保持一致性,是否还应该编写这些方法以instancetype在新代码中明确返回?

- (instancetype)init {
    self = [super init];
    if (self) {
        // perform initialization
    }
    return self;
}
Run Code Online (Sandbox Code Playgroud)

有没有关于为什么或为什么没有或任何推理的文件?在这种情况下,它似乎与编译器完全相同.

objective-c clang instancetype

13
推荐指数
1
解决办法
4842
查看次数

在Objective-C中创建私有属性

在Objective-C中,有一种在.m文件中创建私有方法的hackish方法:

@interface FooClass (PrivateMethods)
- (void) doBar:(id)barAction withBaz:(id)bazAction;
@end
Run Code Online (Sandbox Code Playgroud)

这适用于方法.我试图对一个属性做同样的事情:

@interface BarClass (PrivateMethods)
@property (nonatomic, strong) BazObject *myBaz;
@end

@implementation BarClass
@synthesize myBaz = _myBaz;
[...]
@end
Run Code Online (Sandbox Code Playgroud)

这带来了编译警告:在类'PrivateMethods'中声明的属性无法在类实现中实现.我试图将我的财产转移到一个类别:

@implementation BarClass (PrivateMethods)
@synthesize myBaz = _myBaz;
@end
Run Code Online (Sandbox Code Playgroud)

然后: @synthesize is not allowed in a category's implementation.

答案显然是"停止尝试,只是用实例变量",但我已经被人在苹果告诉他们(个人)完全使用属性感动.他们带来的安全性(如枪上的安全性,更难以在脚中射击自己)让我内心都很开心,所以有没有办法在不借助裸体伊娃的情况下做到这一点?

properties private objective-c

8
推荐指数
1
解决办法
3210
查看次数

使用标记为__unused的参数时发出警告

使用-Wunused参数标志,可以将__unused强制用于未使用的参数,作为编译器优化.以下代码会导致两个警告:

#include <stdio.h>
int main(int argc, char **argv) {
  printf("hello world\n");
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

通过添加__unused未使用的参数来修复这些警告.

#include <stdio.h>
int main(int __unused argc, char __unused **argv) {
  printf("hello world\n");
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

当您使用标记为__unused的参数时,clang 4.1不会发出警告或错误.

#include <stdio.h>
int main(int __unused argc, char __unused **argv) {
  printf("hello world. there are %d args\n", argc);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

使用相同的行为__attribute__((unused)).

int main(int __attribute__((unused)) argc, char __attribute__((unused)) **argv) {
Run Code Online (Sandbox Code Playgroud)

是否有办法在__unused上发出警告或错误?如果您不小心在已使用的参数上留下__unused,会发生什么?在上面的例子中argc似乎有正确的值,虽然它可能是编译器没有利用提示,并且我不会在没有更多理解的情况下依赖这种行为.

c clang

8
推荐指数
1
解决办法
7914
查看次数

使用OCUnit测试是否显示UIAlertView

我正在开发一个应用程序UIAlertView,只有在游戏进度完成时才会显示一个点击它的退出按钮.我想知道如何使用OCUnit拦截UIAlertView并与之交互,甚至检测它是否已经呈现.我唯一能想到的就是monkeypatch [UIAlertViewDelegate willPresentAlertView],但这让我想哭.

有谁知道更好的方法吗?

iphone unit-testing ocunit uialertview

6
推荐指数
1
解决办法
2883
查看次数

iOS 8 CoreBluetooth不推荐使用RSSI方法

所以从阅读CBPeripheralDelegate文档,它看起来RSSIperipheralDidUpdateRSSI:error:被抛弃了与iOS 8.

我注意到我的信号强度指示器不再被更新,所以我做了一些研究并发现了一个新的方法([CBPeripheralDelegate peripheral:didReadRSSI:error:]),它应该在调用readRSSI方法后异步调用.不幸的是,即使我确实将父类设置为CBPeripheral委托,这种方法似乎也没有被回调.

有没有其他人有iOS 8 CoreBluetooth更新的问题?

objective-c ios core-bluetooth ios8

6
推荐指数
1
解决办法
5881
查看次数

在NSArray中存储结构

回到我每天愚蠢的问题.今天我正在尝试在NSArray中放置一个结构.好吧,这很容易,将它包装在NSData中.如果你认识我,你知道你将会看到代码.在这个例子中,我将一个从Wavefront OBJ加载到结构中的顶点线并将其粘贴在一个数组中.

        NSArray *verticeLineParts = [currentLine componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        Coordinate c = CoordinateMake([[verticeLineParts objectAtIndex:1] floatValue],
                                      [[verticeLineParts objectAtIndex:2] floatValue],
                                      [[verticeLineParts objectAtIndex:3] floatValue]);
        [vertices addObject:[[NSData alloc] initWithBytes:&c length:sizeof(c)]];
Run Code Online (Sandbox Code Playgroud)

这似乎工作正常.输出中的前几个对象:

2010-10-18 14:18:08.237 ObjLoader[3778:207] (
    <5d1613bd 13f3da3f 8ac745bd>,
    <f04c28bd 13f3da3f d88048bd>,
    <649427bd d61ddb3f d88048bd>,
    <477625bd d845db3f d88048bd>,
    <4c1722bd 1668db3f d88048bd>,
Run Code Online (Sandbox Code Playgroud)

在从阵列中拉出它们之后将它们打印出来,而不是那么多.违规代码:

for (int i = 0; i < [vertices count]; i++) {
    Coordinate c;
    fillCoordinateFromData(c, [vertices objectAtIndex:i]);
    NSLog(@"%@", CoordinateToNSString(c));
}

[snip]

void fillCoordinateFromData(Coordinate c, NSData *d) {
    void *bytes = malloc(12);
    [d getBytes:bytes];
    memcpy((void *)&c, …
Run Code Online (Sandbox Code Playgroud)

c struct objective-c wavefront nsdata

5
推荐指数
1
解决办法
4394
查看次数

Swift无法将表达式的类型'Void'转换为'String!'类型

我正在尝试使用这篇NSHipster文章中的一段示例代码,大约在页面的一半.

var inputStream: NSInputStream
var outputStream: NSOutputStream

NSStream.getStreamsToHostWithName(hostname: "nshipster.com",
                                      port: 5432,
                               inputStream: &inputStream,
                              outputStream: &outputStream)
Run Code Online (Sandbox Code Playgroud)

我把它放在一个操场上,随之而来,import Foundation我得到了这个错误.

Playground execution failed: error: <REPL>:6:10: error: cannot convert the expression's type 'Void' to type 'String!'
NSStream.getStreamsToHostWithName(hostname: "nshipster.com",
~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

此错误指向第一个参数,这显然是类型String!而不是Void.

我稍微更改了代码,从方法调用中提取定义.这是完整的游乐场:

import Foundation

var inputStream: NSInputStream
var outputStream: NSOutputStream

let host = "nshipster.com"
let port = 5432
NSStream.getStreamsToHostWithName(hostname: host,
    port: port,
    inputStream: &inputStream,
    outputStream: &outputStream)
Run Code Online (Sandbox Code Playgroud)

现在错误表明第三个参数,大概对前两个参数感到满意.

Playground execution failed: error: <REPL>:10:18: error: cannot convert the expression's …
Run Code Online (Sandbox Code Playgroud)

swift

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

估算和衡量CloudKit数据库的使用情况

CloudKit具有一些非常严格的数据限制,限制为50MB的数据库存储加上每个用户1MB,数据库带宽为250KB /天,每个用户为5KB.

要了解CloudKit如何将数据库结构和协议开销合并到这些数字中,是否有一个仪表板,我能够查看记录或表的大小,或消耗的带宽量?

icloud cloudkit

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