小编Dee*_*ora的帖子

为什么以及何时在Swift中使用lazy与Array?

[1, 2, 3, -1, -2].filter({ $0 > 0 }).count // => 3

[1, 2, 3, -1, -2].lazy.filter({ $0 > 0 }).count // => 3
Run Code Online (Sandbox Code Playgroud)

添加lazy到第二个语句的优点是什么?根据我的理解,当使用lazy变量时,内存在使用时被初始化为该变量.在这种情况下它是如何有意义的?

在此输入图像描述

试图LazySequence更详细地了解其用途.我曾使用过的map,reduce并且filter功能上的序列,但从来没有对lazy序列.需要了解为何使用此功能?

arrays lazy-evaluation swift

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

闭包中的弱引用

我需要在闭包内使用self的弱引用.我为此目的使用以下代码:

func testFunction() { 
    self.apiClient.getProducts(onCompletion:  { [weak self] (error, searchResult) in     
        self?.isSearching = false
     }
}
Run Code Online (Sandbox Code Playgroud)

我可以在testFunction的主体中声明self的弱引用,而不是在关闭的捕获列表中给出弱引用.

func testFunction() { 
     weak var weakSelf = self
     self.apiClient.getProducts(onCompletion: {(error, searchResult) in     
         weakSelf?.isSearching = false
     }
}
Run Code Online (Sandbox Code Playgroud)

类似的,Objective-C中也使用了语法来使用块内的弱引用.

__weak typeof(self) weakSelf = self; 
Run Code Online (Sandbox Code Playgroud)

在闭包中通过捕获列表指定弱引用是否有任何优势,而不是在函数体中声明弱变量.如果函数体中有多个闭包,则在函数体中声明弱变量并在所有闭包中使用相同的变量而不是在每个闭包中写入捕获列表更有意义.

closures swift

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

NSURLSession HTTPMaximumConnectionsPerHost 未按预期工作

我正在尝试下载 .m3u8 视频的 .ts 文件。我为每个 .ts url 创建了一个下载任务,并将会话配置 HTTPMaximumConnectionsPerHost 属性设置为 4:

NSURLSessionConfiguration *sessionConfig    = [NSURLSessionConfiguration defaultSessionConfiguration];
  sessionConfig.HTTPMaximumConnectionsPerHost = 4;
_session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:[NSOperationQueue mainQueue]];
Run Code Online (Sandbox Code Playgroud)

预期行为:只能同时下载 4 个 ts,一旦其中任何一个下载完成,下一个下载项目将被放入队列中,以便在任何时候最多有 4 个 ts 正在下载。

实际行为:大约 50 个或更多 ts 正在同时下载,忽略 HTTPMaximumConnectionsPerHost 属性。

Charles 时间线的屏幕截图显示同时发生的多个 .ts 请求。在此输入图像描述

当我尝试使用 NSURLSession 下载图像时,将 HTTPMaximumConnectionsPerHost 指定为 3,我可以看到一次只进行 3 次下载。在此输入图像描述

要下载 m3u8,我还可以使用 AVAssetDownloadURLSession 而不是 NSURLSession,后者一次仅下载 1 个 .ts。在此输入图像描述

我想找出:

1) 为什么 HTTPMaximumConnectionsPerHost 属性对于图像下载可以正常工作,而对于 .ts 下载则不起作用,因为会同时发生超过 4 个 .ts 下载。

2)有没有办法使用AVAssetURLDownloadSession将最大并发.ts下载增加到4,它只下载1.ts

ios nsurlsession nsurlsessionconfiguration nsurlsessiondownloadtask

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

如何使用expo从文件系统读取jpg文件?

我使用以下代码使用 expo 下载了一个图像(a.jpg):

 FileSystem.downloadAsync(
                    httpUrl,
                    FileSystem.documentDirectory + location
                ).then((result)=>{
                    const uri = result.uri;
                }).catch((err)=>{
                    console.log("?getFile -> err", err);}
);
Run Code Online (Sandbox Code Playgroud)

文件成功保存在文件系统中。稍后当我尝试读取文件时,出现无法读取文件的错误。用于读取文件的代码:

const fileInfo = await FileSystem.getInfoAsync(uri);
if(fileInfo.exists){
            FileSystem.readAsStringAsync(uri).then(data => {
                const base64 = 'data:image/jpg;base64' + data;
                resolve(data) ; 
            }).catch(err => {
                console.log("?getFile -> err", err);
                reject(err) ;
            });
 }
Run Code Online (Sandbox Code Playgroud)

上面的代码返回无法读取文件的错误。fileInfo.exists 为真,因为文件存在于文件系统中。

 ?getFile -> fileInfo Object {
    "exists": 1,
     "isDirectory": false,
     "modificationTime": 1547272322.8714085,
     "size": 51725,
    "uri": "file:///Users/deeparora/Library/Developer/CoreSimulator/Devices/A2DC4519-       C18C-4512-8C23-E624A1DAA506/data/Containers/Data/Application/6D7B23AA-      A555-4F9A-B9D1-EB5B9443CCB6/Documents/ExponentExperienceData/       %2540anonymous%252Fhola-vet-6faee8ac-e309-4d5b-a1c0-6f8688f8a508/a.jpg",
:}
Run Code Online (Sandbox Code Playgroud)

读取文件时出错:

err [Error: File 'file:///Users/deeparora/Library/Developer/CoreSimulator/Devices/A2DC4519-C18C-4512-8C23-E624A1DAA506/data/Containers/Data/
Application/6D7B23AA-A555-4F9A-B9D1-EB5B9443CCB6/
Documents/ExponentExperienceData/%2540anonymous%252Fhola-vet-6faee8ac-e309-4d5b-a1c0-6f8688f8a508/a.jpg' could not be read.] …
Run Code Online (Sandbox Code Playgroud)

react-native expo

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

Swift中Pointwise Equal,Pointwise小于和Pointwise大的功能是什么?

在阅读Apple的Swift编程语言书时,我遇到了Pointwise等于,Pointwise小于和Pointwise大于运算符。参考:https : //developer.apple.com/documentation/swift/swift_standard_library/operator_declarations

.== Pointwise equal

.!= Pointwise not equal
Run Code Online (Sandbox Code Playgroud)

我找不到有关何时使用它们的任何解释和示例。这些操作员的功能是什么?

swift

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

如何在AWS AppSync控制台中测试订阅?

我已在App Sync控制台中编写了以下订阅和变异代码:

subscription SubscribeToCreateDoctor {

  subscribeToCreateDoctor {
       id
       name
  }

}

mutation CreateDoctor {

      createDoctor(
        input: {
          name: "sanju", 
          registrationNo: "some value",
          speciality: "some value",
          profilePic: "some value",
          placeOfResidence: "some value", 
          medicalCenter: "some value",
          direction: "some value",
          municipality: "some value",
          isAvailable: "No",
        }) {
         id
         name

       }
}
Run Code Online (Sandbox Code Playgroud)

在模式中,我同时定义了变异和订阅:

type Subscription {

    subscribeToCreateDoctor: Doctor
        @aws_subscribe(mutations: ["createDoctor"])
}

type Mutation {

    createDoctor(input: CreateDoctorInput!): Doctor

}
Run Code Online (Sandbox Code Playgroud)

当我在App Sync控制台中测试CreateDoctor突变时,得到以下响应:

{
  "data": {
    "createDoctor": {
      "id": "5845c994-2389-4df9-8a3e-e13dc24b0153",
      "name": "Sanju"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,我没有看到在AWS App …

amazon-web-services aws-appsync

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

左上角像素坐标 ios (0,0) 还是 (1,1)?

考虑到ios设备的屏幕分辨率是(320*480)点。左上角像素的点值是多少?是 0,0 还是 1,1 ?

ios

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

如何从大小约2mb的csv文件创建NSString?

我有一个大约2.2 MB数据的csv文件存在于应用程序包中.我试图将此csv文件的内容存储在NSString变量中.代码用于:

   NSString *strBundle = [[NSBundle mainBundle] pathForResource:@"large" ofType:@"csv"];
   NSError *error = nil;
   NSString *dataStr = [NSString stringWithContentsOfFile:strBundle
                                                  encoding:NSUTF8StringEncoding
                                                     error:&error];
Run Code Online (Sandbox Code Playgroud)

但是,上面的代码不会将文件的文本存储在dataStr变量中.相反,我收到以下错误消息:

错误=错误域= NSCocoaErrorDomain代码= 261"操作无法完成.(Cocoa错误261.)"UserInfo = 0x1fdab580 {NSFilePath =/var/mobile/Applications/0C68E393-FBBE-45F6-819E-336D31C78043/DemoApp. app/large.csv,NSStringEncoding = 4}

如果,我删除文件的内容,使文件相对小于2.2 MB,上述代码工作正常,并获取dataStr变量中的字符串值.有人可以告诉我为什么会发生这种情况.可以存储在NSString变量中的数据量是否有任何限制.如何存储CSV文件的数据呢?

objective-c ios

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