小编Rob*_*Rob的帖子

DispatchQueue 的行为不像串行队列

通过使用以下代码,我期望每次调用 func 时,执行之间的最短时间为 3-4 秒。

但是:当我writeData()连续调用4 次时,我看到异步块在不等待前一次调用完成的情况下执行。

func writeData(){
    DispatchQueue(label: "be.io").asyncAfter(deadline: .now() + 1) {
        print("START :\(Int64((Date().timeIntervalSince1970 * 1000.0).rounded()))")
        Thread.sleep(forTimeInterval: 3)
    }
}
...
writeData()
writeData()
writeData()
writeData()
Run Code Online (Sandbox Code Playgroud)

预期的输出应该是这样的:

START :1611250630000
START :1611250634000
START :1611250638000
START :1611250642000
Run Code Online (Sandbox Code Playgroud)

但在执行时,我得到了所有 4 个调用相同的时间戳(大约 1 毫秒的差异)。

START :1611250630000
START :1611250630000
START :1611250630000
START :1611250630000
Run Code Online (Sandbox Code Playgroud)

我在做什么错?

PS我毫不拖延地尝试了异步,没有运气。

grand-central-dispatch ios swift dispatch-queue

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

无法打开阿拉伯文本文件

我正在尝试.srt使用以下命令打开文件String

\n
let str = try String(filePath , encoding : .utf8)\n
Run Code Online (Sandbox Code Playgroud)\n

但每次我想读取特定.srt文件时都会出现此错误:

\n
\n

无法使用文本编码打开\xe2\x80\x99

\n
\n

样本文件

\n

问题是什么?

\n

string arabic swift

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

设置进程退出的等待时间

我使用以下命令来运行bat文件:

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.FileName = "d:/my.bat";
p.Start();
p.WaitForExit(2000000);               
p.Close();
p.Dispose();
Run Code Online (Sandbox Code Playgroud)

我的问题是我需要等到上述过程完成并尽快关闭它.

有什么建议?

.net c# batch-file

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

如何在tableview中选择新行时取消选择所选行?

我有一个应用程序,当用户选择一个新单元格时,我需要取消选择所选单元格.我可以选择和取消选择tableview行,但问题是我需要一次只选择一行.我现在这样做,

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
   cell.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"white_cell_check.png"]];   

}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
      UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

     cell.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"white_cell.png"]]; 

}
Run Code Online (Sandbox Code Playgroud)

但在这个我可以选择和取消选择相同的行.但我的意图是在选择新行时,如果已选择任何其他单元格,则需要取消选择.有谁能够帮我 ?

iphone ipad ios

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

如何防止iOS 7 NSURLConnection使用缓存

问题:这个过程与iOS6完美配合.现在它不会提取最新信息并替换显示的数据.因此,应用程序信息总是错误的.如果我重置模拟器上的内容,它可以在当前时间段内工作.之后我将需要重新设置内容.有什么建议.我已经包含了处理我所有数据处理的3个文件以供参考.

Reader.M

- (void) startRequest
{
    NSURL *url = [[NSURL alloc] initWithString:@"http://www.something.com/app/rss/"];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:60.0f];
    NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES];
    if(conn)
    {
        _receivedData = [[NSMutableData alloc]init];
    }
}

#pragma mark - NSURLConnectionDataDelegate

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"%@",error.description);
}

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"received %d bytes",data.length);
    [_receivedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:_receivedData];
    JSTRRSSParser *rssParser = [[JSTRRSSParser alloc] init];
    parser.delegate = rssParser; …
Run Code Online (Sandbox Code Playgroud)

nsdictionary nsxmlparser nsmutablearray ios

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

Objective C如何在类方法中引用视图

我的应用程序有多个视图以编程方式添加按钮,而不是在每个控制器中使用以下实例方法,我想将该方法放在一个公共类中,将其用作类方法.

+ (void) addLeftImageButton:(UIButton *)leftButton:(float)x:(float)y:(NSString *)name:(int) tag
{
     leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
     [leftButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
     [leftButton setTag: tag];
     [leftButton setBackgroundImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
     leftButton.frame = CGRectMake(x, y, 345.0, 345.0);
     [self.view addSubview:leftButton];
}
Run Code Online (Sandbox Code Playgroud)

我的问题是如何引用self.view调用视图.如self.view没有在普通类公知的.

objective-c uiview ios

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

通过核心图形绘制自定义形状的圆角

我正在使用Core Graphics绘制自定义形状,我想为这个形状制作圆角,这是我绘制自定义形状的代码

CGPoint p1=[self getPointFromAngleQuarter:start_angle2 andRaduis:card.small_Raduis andCenter:center];
CGContextMoveToPoint(context, p1.x, p1.y);
CGPoint p2=[self getPointFromAngleQuarter:start_angle2 andCenter:center andRaduis:self.large_Raduis];
CGContextAddLineToPoint(context, p2.x, p2.y);
CGContextAddArc(context,center.x, center.y, selectedLargeRaduis, start, end,0);
CGPoint p5=[self getPointFromAngle:end_Angle andCenter:center andRaduis:self.small_Raduis];
CGContextAddLineToPoint(context, p5.x, p5.y);
CGContextAddArc(context,center.x, center.y,selectedSmallRaduis, end, start,1);
CGContextDrawPath(context, kCGPathFill);
Run Code Online (Sandbox Code Playgroud)

这是我自定义Shape的最终结果

自定义形状:

自定义形状

core-graphics cgcontext ios

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

如何在Swift中从负数更改为零

我有Double数据类型,因为我需要带有浮点数的结果,但是如果我的结果是否定的,则会破坏我的所有算法.是否可能存在具有浮点的无符号数据类型?

在此输入图像描述

unsigned-long-long-int swift negative-zero

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

Dispatch.main.async如何"更新UI"?

我一直在使用Swift一段时间,而GCD仍然让我感到困惑.

我读了:

https://www.raywenderlich.com/60749/grand-central-dispatch-in-depth-part-1

以及关于发送的Apple文档:

https://developer.apple.com/documentation/dispatch

我理解GCD允许在不同线程上运行多个任务的整体概念(我认为是正确的).

我不太明白的是Dispatch.main.async"更新UI".

例如,如果我在某处调用api并返回数据 - 比如返回所有数据需要5秒钟,那么如何使用Dispatch.main.async帮助更新UI?如何Dispatch.main.async知道要更新的UI?

而且我仍然不能完全取代GCD,为什么不能在加载所有数据时使用某种观察者或委托或闭包?

如果我正在进行api调用但不立即使用数据,请重新:用GCD"更新UI".只是将数据存储在一个数组中,直到我决定使用它,那么是否需要使用Dispatch.main.async

我现在一直在使用firebase/firestore作为数据库.Firebase拥有自己的侦听器并以异步方式运行.我仍然无法得到一个很好的答案:在iOS/Swift中处理来自firebase的异步返回的最佳方法.例如,当我的应用程序加载,如果我去firebase获取数据来填充tableviewcontroller,什么是知道所有数据何时返回的最佳方式?我一直在使用代表,但想知道是否以及如何Dispatch.main.async使用.

grand-central-dispatch firebase swift

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

UIBezierPath - 波形/噪声数据的平滑图表

我的目标是在用户在屏幕上滑动时实现类似Adobe Illustrator的行.目前它非常不稳定 - 这是当前情况的截图:

嘈杂的线条

我认为这显然是我想要的.我希望平滑这些直线.这叫做抗锯齿吗?

- (void)touchesBegan: (CGPoint)point
{
    currentStroke = [[Stroke alloc] initWithStartPoint:point];
    [strokes addObject:currentStroke];
}

- (void)touchesMoved: (CGPoint)point
{
    if (!currentStroke) {
        [self touchesBegan:point];
    }
    [currentStroke.points addObject:[NSValue valueWithCGPoint:point]]
    pathTwo = [self createPath];
    [self scaleToFit];
    [self setNeedsDisplay];
}

- (UIBezierPath *)createPath {
    UIBezierPath * bezierPath = [UIBezierPath bezierPath];
    for (Stroke * stroke in strokes) {
        [bezierPath moveToPoint:stroke.startPoint];
        for (NSValue * value in stroke.points) {
            [bezierPath addLineToPoint:[value CGPointValue]];
        }
    }
    return bezierPath;
}

- (void)touchesEnded: (CGPoint)point
{
    [points removeAllObjects];
    [self …
Run Code Online (Sandbox Code Playgroud)

objective-c ios uibezierpath

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