小编cre*_*orn的帖子

ARC,自我和块

我以为我理解在复制的块中使用self是不可能的.

但是为了清理我的代码,我在Xcode中启用了一堆警告,一个叫做"向弱指针发送消息"

所以现在在我的所有块中,每次我使用我创建的weakself引用__weak typeof(self) weakself = self;

我收到这个警告: Weak receiver may be unpredictably set to nil

一个简单的例子:

__weak typeof(self) weakself = self;
[aClass doSomethingInABlock:^{

     [weakself doSomething]; //warning.

}];
Run Code Online (Sandbox Code Playgroud)

我已经看到了在块中定义自我版本的答案,如下所示:

__weak typeof(self) weakself = self;
[aClass doSomethingInABlock:^{
     typeof(self) selfref = weakself; 
     [selfref doSomething]; //no warning.

}];
Run Code Online (Sandbox Code Playgroud)

所以我想知道这里到底发生了什么:

  • 我只是欺骗编译器?
  • 强引用弱引用有什么作用?
  • 我缺少的任何其他东西.

谢谢.

objective-c ios objective-c-blocks automatic-ref-counting retain-cycle

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

next_permutation返回奇怪的结果

我试图找到给定字符列表的所有排列,在这种情况下'eta'

std::string s="eta";
do
{
    std::cout<<s<<std::endl;

}while(std::next_permutation(s.begin(),s.end()));
Run Code Online (Sandbox Code Playgroud)

我将得到以下输出:

eta
tae
tea
Run Code Online (Sandbox Code Playgroud)

但如果我改变了一件事

std::string s="eta";
Run Code Online (Sandbox Code Playgroud)

std::string s="aet";
Run Code Online (Sandbox Code Playgroud)

输出现在变成了

aet
ate
eat
eta
tae
tea
Run Code Online (Sandbox Code Playgroud)

这是我期望的正确排列数; 因此,当"加扰"字符串按字母顺序排列时,显然会出现一些不同的情况?

或者这可能是造成这种歧义的原因?

c++ stl permutation

2
推荐指数
1
解决办法
124
查看次数

sudo gem install jekyll - 失败 - clang错误未知参数

Jekyll不会通过gem install以下安装是错误.下面还有一些版本的东西和位置.

sudo gem install jekyll                                                                                                                                    
Building native extensions.  This could take a while...
ERROR:  Error installing jekyll:
ERROR: Failed to build gem native extension.

/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb
creating Makefile

make "DESTDIR=" clean

make "DESTDIR="
compiling porter.c
porter.c:359:27: warning: '&&' within '||' [-Wlogical-op-parentheses]
  if (a > 1 || a == 1 && !cvc(z, z->k - 1)) z->k--;
            ~~ ~~~~~~~^~~~~~~~~~~~~~~~~~~~
porter.c:359:27: note: place parentheses around the '&&' expression to silence this warning
  if (a > 1 || a == 1 …
Run Code Online (Sandbox Code Playgroud)

ruby gem clang

2
推荐指数
1
解决办法
737
查看次数

未释放的class_copyPropertyList上的内存泄漏

所以,分析我的应用程序有些泄漏.一个人objc_property_t *properties = class_copyPropertyList(self.class, NULL);没有被释放.

但是当我补充说 free(properties)

alloc: *** error for object 0x10b773f58: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug

代码看起来像这样:

- (void) encodeWithCoder:(NSCoder *)aCoder {

    objc_property_t *properties = class_copyPropertyList(self.class, NULL);

    while (*properties != '\0') {

        NSString *pname = [NSString stringWithUTF8String:property_getName(*properties)];
        const char * attrs = property_getAttributes(*properties);

        // do something here and set the value to be encoded

        }
        properties ++;
    }
    free(properties);
}
Run Code Online (Sandbox Code Playgroud)

objc_property_t类型的指针数组,描述类声明的属性.超类声明的任何属性都不包括在内.该数组包含*outCount指针,后跟一个NULL终止符.您必须使用free()释放数组.

代码工作,但泄漏.现在,当我添加free(properties)它崩溃.

memory-leaks runtime objective-c

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

javascript类型问题:未捕获TypeError:对象0没有方法'draw'

刚尝试使用JS + canvas,我似乎已经碰壁了.我的最小应用程序的"目标"是在画布上的任意随机位置单击,按下绘制按钮并在您单击的位置绘制正方形.

来自OO背景...我(尝试)使用OO,这在js我完全没有掌握.

但基本上我有一个自定义Square对象

function Square(l, w, x, y) {

    this.length = l;
    this.width  = w;
    this.posx   = x - l/2;
    this.posy   = y - w/2;

    //test
    //ctx.fillStyle = "rgb(20,0,0)";
    //ctx.fillRect(this.posx,this.posy,this.length,this.width);


    this.draw = function() {

        ctx.fillStyle = "rgb(20,0,0)";
        ctx.fillRect(this.posx,this.posy,this.length,this.width);

    }
}
Run Code Online (Sandbox Code Playgroud)

我每次用户点击时都会添加一个数组...这里是我点击画布时的事件处理程序.

function addTo(evt) {

    pos = getMousePos(evt);
    var sq = new Square(50, 50, pos.x, pos.y);
    list.push(sq);

    output.innerText = "("+sq.posx+","+sq.posy+")";
}
Run Code Online (Sandbox Code Playgroud)

这是我(尝试)绘制正方形的地方.

function renderStack() {

    //alert(list);
    canvas.width = canvas.width;
    for(var o in list) o.draw();

}
Run Code Online (Sandbox Code Playgroud)

这是错误:

Uncaught TypeError: Object …
Run Code Online (Sandbox Code Playgroud)

javascript oop canvas

0
推荐指数
1
解决办法
2881
查看次数

dispatch_sync中的死锁

以下代码中的死锁.为什么?

dispatch_queue_t queue = dispatch_get_main_queue(); 
dispatch_async(queue, ^{
    dispatch_sync(queue, ^{
        NSLog(@"Hello?");
    }); 
});
Run Code Online (Sandbox Code Playgroud)

grand-central-dispatch ios

0
推荐指数
1
解决办法
145
查看次数