小编onm*_*133的帖子

如何将现有的回调API转换为承诺?

我想使用promises,但我有一个回调API,格式如下:

1. DOM加载或其他一次性事件:

window.onload; // set to callback
...
window.onload = function() {

};
Run Code Online (Sandbox Code Playgroud)

2.平原回调:

function request(onChangeHandler) {
    ...
}
request(function() {
    // change happened
    ...
});
Run Code Online (Sandbox Code Playgroud)

3.节点样式回调("nodeback"):

function getStuff(dat, callback) {
    ...
}
getStuff("dataParam", function(err, data) {
    ...
})
Run Code Online (Sandbox Code Playgroud)

4.具有节点样式回调的整个库:

API;
API.one(function(err, data) {
    API.two(function(err, data2) {
        API.three(function(err, data3) {
            ...
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

如何在promises中使用API​​,我该如何"宣传"它?

javascript callback node.js promise bluebird

680
推荐指数
12
解决办法
20万
查看次数

iOS 7中导航栏的高度是多少?

我刚刚使用默认的Master Details模板创建了一个简单的iOS 7应用程序.

MasterViewController.m,viewDidAppear方法,我登录了

self.navigationController.navigationBar.frame.size.height
self.navigationController.navigationBar.frame.origin.y
Run Code Online (Sandbox Code Playgroud)

并因此收到44和20.这看起来很奇怪,因为根据我的阅读,iOS 7中的导航栏应该是64点高度并从0点开始,因此它位于状态栏下方.我读到的是

1)iOS 7 UI过渡指南

因为状态栏是透明的,所以它后面的视图显示出来

2)在导航栏中使用背景图像

如果您的应用使用自定义图像作为条形图的背景,则需要提供"更高"的图像,使其向上延伸到状态栏后面.导航栏的高度从44点(88像素)更改为64点(128像素).

height frame statusbar navigationbar ios7

69
推荐指数
2
解决办法
13万
查看次数

WebRTC使用TCP还是UDP?

这听起来像一个非常基本的问题,但我需要确认

  1. WebRTC是否使用TCP或UDP作为其对等传输?我怎么知道 ?
  2. 我看到有可靠性模式和DTLS协议,它们如何影响?
  3. Media和DataChannel的传输方式是否相同?
  4. 如何在TCP和UDP之间切换?

我问这个是因为我知道浏览器对并行连接的数量有限制(我认为他们谈论TCP),并且UDP连接可能不受限制.

udp tcp channel transport webrtc

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

什么是暂停功能在Kotlin Coroutine中意味着什么

我正在读Kotlin Coroutine并知道它是基于suspend功能的.但是什么suspend意思呢?

协程或功能被暂停?

来自https://kotlinlang.org/docs/reference/coroutines.html

协同程序基本上是协程,可以在不阻塞线程的情况下暂停

我听说人们经常说"暂停功能".但是我认为正在等待函数完成的是协同程序被暂停?"暂停"通常意味着"停止操作",在这种情况下,协程是空闲的.

我们应该说协程被暂停吗?

哪个协程被暂停?

来自https://kotlinlang.org/docs/reference/coroutines.html

为了继续这个类比,await()可以是一个挂起函数(因此也可以从async {}块中调用),它挂起一个协同程序,直到完成一些计算并返回其结果:

async { // Here I call it the outer async coroutine
    ...
    // Here I call computation the inner coroutine
    val result = computation.await()
    ...
}
Run Code Online (Sandbox Code Playgroud)

它说"在完成某些计算之前暂停协程",但协同程序就像一个轻量级的线程.因此,如果协程被暂停,那么计算怎么办呢?

我们看到await被调用computation,所以它可能会async返回Deferred,这意味着它可以启动另一个协同程序

fun computation(): Deferred<Boolean> {
    return async {
        true
    }
}
Run Code Online (Sandbox Code Playgroud)

引述说暂停协程.它是指suspend外部的asynccoroutine,还是suspend内部的computationcoroutine?

suspend意味着当外部async协程正在wait(await …

suspend async-await kotlin kotlin-coroutines

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

在ReactJS中导出(默认)类

如果我正在创建一个组件,似乎你可以用很多不同的方式创建一个类.这些有什么区别?我怎么知道使用哪一个?

import React, {Component} from 'react'

export default class Header extends Component {

}

export const Header = React.createClass({

})

export default React.createClass({

})
Run Code Online (Sandbox Code Playgroud)

我只是假设他们做了不同的事情,还是仅仅是不同的语法?

如果有人能给我一个快速解释或链接,我会非常感激.我不想从一个新的框架开始,不知道究竟有什么区别.

javascript syntax ecmascript-6 reactjs

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

使用符合协议AnyObject的具体类型不受支持

我正在使用Swift 2并使用WeakContainer作为存储一组弱对象的方式,就像 NSHashTable.weakObjectsHashTable()

struct WeakContainer<T: AnyObject> {
    weak var value: T?
}

public protocol MyDelegate : AnyObject {

}
Run Code Online (Sandbox Code Playgroud)

然后在我的ViewController中,我声明

public var delegates = [WeakContainer<MyDelegate>]
Run Code Online (Sandbox Code Playgroud)

但这是错误的

使用MyDelegate作为符合协议AnyObject的具体类型不受支持

我看到的错误是,WeakContainer具有value声明为成员weak,因此T有望成为对象.但我也声明MyDelegateAnyObject也.怎么解决这个问题?

protocols reference hashtable weak swift

33
推荐指数
3
解决办法
8128
查看次数

编辑Xcode xcconfig文件和Cocoapods

我想编辑xcconfig文件,但遗憾的是Xcode 每个配置只允许1个xcconfig文件,那就是Pods.xcconfig,因为我正在使用Cocoapods

那么如何编辑xcconfig而不会伤害Cocoapods

我可以想到几种方法

  1. 使Xcode使用多个xcconfig文件
  2. 使用我自己的包含Pods.xcconfig文件的xcconfig文件

那么如何应对呢?

xcode xcconfig ios cocoapods

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

5种魔法记录设置方法有什么区别?

任何人都可以给出5种设置方法的描述吗?

(void) setupCoreDataStack;
(void) setupAutoMigratingDefaultCoreDataStack;
(void) setupCoreDataStackWithInMemoryStore;
(void) setupCoreDataStackWithStoreNamed:(NSString *)storeName;
(void) setupCoreDataStackWithAutoMigratingSqliteStoreNamed:(NSString *)storeName;
Run Code Online (Sandbox Code Playgroud)

它们各自做了什么以及每个用例的用例是什么?

database core-data objective-c ios magicalrecord

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

如何使UIImage符合Codable?

Swift 4有Codable,真棒.但UIImage默认情况下不符合它.我们怎么做?

我试过singleValueContainerunkeyedContainer

extension UIImage: Codable {
  // 'required' initializer must be declared directly in class 'UIImage' (not in an extension)
  public required init(from decoder: Decoder) throws {
    let container = try decoder.singleValueContainer()
    let data = try container.decode(Data.self)
    guard let image = UIImage(data: data) else {
      throw MyError.decodingFailed
    }

    // A non-failable initializer cannot delegate to failable initializer 'init(data:)' written with 'init?'
    self.init(data: data)
  }

  public func encode(to encoder: Encoder) throws {
    var …
Run Code Online (Sandbox Code Playgroud)

uiimage swift4 codable

15
推荐指数
6
解决办法
8218
查看次数

连续语音识别.与SFSpeechRecognizer(ios10-beta)

我正在努力执行续.AVCapture在iOS 10 beta上使用语音识别.我已经设置captureOutput(...)了不断获得CMSampleBuffers.我将这些缓冲区直接放入SFSpeechAudioBufferRecognitionRequest我之前设置的中,如下所示:

... do some setup
  SFSpeechRecognizer.requestAuthorization { authStatus in
    if authStatus == SFSpeechRecognizerAuthorizationStatus.authorized {
      self.m_recognizer = SFSpeechRecognizer()
      self.m_recognRequest = SFSpeechAudioBufferRecognitionRequest()
      self.m_recognRequest?.shouldReportPartialResults = false
      self.m_isRecording = true
    } else {
      print("not authorized")
    }
  }
.... do further setup


func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {

if(!m_AV_initialized) {
  print("captureOutput(...): not initialized !")
  return
}
if(!m_isRecording) {
  return
}

let formatDesc = CMSampleBufferGetFormatDescription(sampleBuffer)
let mediaType = CMFormatDescriptionGetMediaType(formatDesc!)
if (mediaType …
Run Code Online (Sandbox Code Playgroud)

beta ios swift ios10

14
推荐指数
4
解决办法
2万
查看次数