小编Nor*_*der的帖子

如何在Javascript中将字符串转换为可执行的代码行?

我有以下一点代码

console.log("I am");

var x = "console.log('Alive!')";
Run Code Online (Sandbox Code Playgroud)

现在我只想用来x执行分配给它的代码字符串 - 我可能甚至不知道x的值,但只是想要执行它可能的任何东西 - 这可能吗?

javascript

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

在这种情况下主队列/当前队列和主线程/后台线程之间的区别?

我正在执行以下方法:

MotionHandler.m

-(void)startAccelerationUpdates
{
    [motionManagerstartDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]withHandler:^(CMDeviceMotion *motion, NSError *error){.....}
}
Run Code Online (Sandbox Code Playgroud)

在后台线程上,如下所示:

[currentMotionHandler performSelectorInBackground:@selector(startAccelerationUpdates) withObject:nil];
Run Code Online (Sandbox Code Playgroud)

但是上面的方法使用main Queue(在主线程上)来执行必要的更新,即使我在后台线程上调用它.所以加速更新是在后台线程或主线程上执行的,I'我很困惑..?

更有趣的是,当我再次在后台线程上调用上面的方法时,但是这次使用了current Queue,我得不到更新.有人可以解释一下运行之间的区别:

 1. a background thread but on the main queue

 2. a background thread but on the current queue 

 3. the main thread but on the main queue  

 4. the main thread but on the current queue
Run Code Online (Sandbox Code Playgroud)

在目前的实施?谢谢!

queue multithreading objective-c ios

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

如何突然停止当前在后台执行的线程?

事实:

我有一个在后台线程上执行的方法:

[currentGame performSelectorInBackground:@selector(playOn:) withObject:self];
Run Code Online (Sandbox Code Playgroud)

这个方法基本上包含一个while循环,一直持续执行,直到用户点击Quit按钮:

-(void) playOn: (UIViewController*) thisViewController
{
    while(!quitButtonPressed)
    {
       // this is my method's main loop
    }
}
Run Code Online (Sandbox Code Playgroud)

问题:

如果用户在上述循环中间的某处单击Quit,则循环 的其余部分必须再次检查BOOL 之前执行并最终停止.为了防止这种情况发生,并具有while循环停止,只要用户点击退出,我想我还可以添加很多 if(quitButtonPressed) break; 在这里和那里,我while循环,以半经常检查,"立即"割舍如果需要的话.然而,考虑到上面的主要while循环的大小以及它内部包含许多较小的while循环这一事实,从设计角度来看,这似乎并不是非常聪明或实用(if.. break;我必须添加的数量将是弄得很大,可能会让事情变得很复杂......

可能的解决方案(但它是正确的解决方案吗?):

所以我认为最好的方法是停止/取消上面方法的while循环执行的后台线程,而不是while循环本身,在方法内,用户点击Quit的那一刻

是这样的,或类似的(即更好的建议),可能,我怎么能这样做?

上述解决方案的可能实施:

我可以创建这个新方法:

-(void)checkQuitButton
{
   while(!quitButtonPressed)
   {
      //wait
   }
   if(quitButtonPressed)
   {
     // stop this thread-->[currentGame performSelectorInBackground:@selector(playOn:) withObject:self];
     // this is the method …
Run Code Online (Sandbox Code Playgroud)

multithreading objective-c while-loop ios

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

我如何计算iOS中的时间?

在一段时间过后,我不想设置一个"触发"(并做某事)的计时器.

因此,我对NSTimer课程及其方法不感兴趣.

所有我感兴趣的是计算经过的时间,例如,一些while循环正在执行.NSDate我猜可以使用如下:

NSDate currentDate = [[NSDate alloc] init];

while(someConditionIsTrue)
{
    // executing..
}

timeElapsed = [self timeIntervalSinceDate:currentDate];

NSLog(@"time elapsed was: %i", timeElapsed);
Run Code Online (Sandbox Code Playgroud)

但是,如果我理解正确,timeIntervalSinceDate:只能计算秒数.

有没有办法可以计算在几毫秒内经过的时间?

换句话说,我可以计算的最小单位是什么?

objective-c nsdate nstimer nstimeinterval ios

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

NSLog是否可以只传递我传递的字符串,而不是其他信息?

的NSLog(@ "例如");

将输出:

2012-09-13 04:54:48.128 MyApp [94652:a0f]:例子

在控制台上.

有没有办法让我只能输出:

xcode objective-c nslog

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