小编jua*_*njo的帖子

如果list中的列为空,则抛出哪个异常?

如果列表为空,我有关于抛出哪个异常的疑问

public class XYZ implements Runnable {
    private List<File> contractFileList;

    @Override
    public void run() { 
        contractFileList = some method that will return the list;
        //now i want to check if returned contractFile is empty or not , if yes then raise the exception
        if (contractFileList.isEmpty()) {
            // throw new ?????
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我在批处理中运行此代码,我想抛出一些将停止批处理执行的异常.

java

24
推荐指数
2
解决办法
3万
查看次数

向子视图添加约束会使背景颜色不显示

所以我使用自定义函数来格式化我添加到a的子视图UICollectionViewCell.它来自Brian Voong的公共项目:https://github.com/purelyswift/facebook_feed_dynamic_cell_content/blob/master/facebookfeed2/ViewController.swift.

func addConstraintsWithFormat(format: String, views: UIView...) {
   var viewsDictionary = [String: UIView]()
   for (index, view) in views.enumerate() {
      let key = "v\(index)"
      viewsDictionary[key] = view
      view.translatesAutoresizingMaskIntoConstraints = false
   }
   addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(format, options: NSLayoutFormatOptions(), metrics: nil, views: viewsDictionary))
}
Run Code Online (Sandbox Code Playgroud)

有趣的是,在我的UICollectionViewI中添加一个SubView单元格,并将背景颜色设置为白色.当我注释掉设置子视图背景的线时,背景为白色,当我取消注释设置子视图的可视格式约束的线时,没有设置背景颜色.

以下两行相互冲突:

func chronicleOneClicked(sender: UIButton) {
   point1view.backgroundColor = UIColor.whiteColor()

   addSubview(point1view)
   //When the below is commented the background of point1view disappears   
   //addConstraintsWithFormat("|-50-[v0]-50-|", views: point1view)
}
Run Code Online (Sandbox Code Playgroud)

当我这样做时,print(subviews)我看到UIView白色背景颜色是视图堆栈中最高的(堆栈顶部).当我打印出来时,subviews[subviews.count-1].backgroundColor我得到的Optional(UIDeviceWhiteColorSpace …

uiview ios swift

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

当我试图在swift中绘制渐变时出现问题

我在Swift代码中尝试绘制渐变时遇到错误:

GradientView.swift:31:40:找不到接受提供的参数的'__conversion'的重载

这是我的代码:

let context: CGContextRef = UIGraphicsGetCurrentContext()
let locations: CGFloat[] = [ 0.0, 0.25, 0.5, 0.75 ]
let colors = [UIColor.redColor().CGColor, UIColor.greenColor().CGColor,UIColor.blueColor().CGColor, UIColor.yellowColor().CGColor]
let colorspace: CGColorSpaceRef = CGColorSpaceCreateDeviceRGB()
let gradient: CGGradientRef = CGGradientCreateWithColors(colorspace, colors, locations)
//CGGradientCreateWithColors(colorspace,colors,locations)
let startPoint: CGPoint = CGPointMake(0, 0)
let endPoint: CGPoint = CGPointMake(500,500)
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
Run Code Online (Sandbox Code Playgroud)

问题是CGGradientCreateWithColors采用CFArray而不是普通的Swift数组.我不知道如何将CFArray转换为Array,并且在Apple的文档中找不到任何内容.任何的想法?谢谢

core-graphics ios swift

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

fusedLocationProviderClient 带有未决意图的 requestLocationUpdates 不会触发

我正在尝试在后台运行位置跟踪器。为此,我读到我需要将endingIntent与fusedLocationProviderClient一起使用,以便我的应用程序将继续每x秒查询用户的位置,即使应用程序被最小化/关闭。

我正在使用此存储库中的代码https://github.com/googlesamples/android-play-location/tree/master/LocationUpdatesPendingIntent/app/src/main/java/com/google/android/gms/location/sample/位置更新消费意图

我的 mapActivity 在 kotlin 中。问题是,当我使用pendingIntent启动fusedLocationProviderClient时,我似乎无法触发任何位置更新(如果我使用回调在主线程中运行它,它会很好地工作,但是,这会减慢我的手机速度)。

有谁知道如何让 fusedLocationProviderClient 在我的情况下与未决意图一起工作?

我尝试使用intent.setAction() 中的字符串,使用我的包名称“com.russ.locationalarm”并在末尾附加“.action.PROCESS_UPDATES”。我对意图不熟悉,所以我不确定这是否是正确使用的字符串。我也尝试过将 PendingIntent.GetService() 切换为 PendingIntent.getBroadCast(),但似乎都不起作用。

这是我启动 fusedLocationProviderClient 的函数:

private fun startLocationUpdates() {

    // Create the location request to start receiving updates

    mLocationRequest!!.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
    mLocationRequest!!.setInterval(INTERVAL)
    mLocationRequest!!.setFastestInterval(FASTEST_INTERVAL)

    // Create LocationSettingsRequest object using location request
    val builder = LocationSettingsRequest.Builder()
    builder.addLocationRequest(mLocationRequest!!)
    val locationSettingsRequest = builder.build()

    val settingsClient = LocationServices.getSettingsClient(this)
    settingsClient.checkLocationSettings(locationSettingsRequest)

    mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
    // new Google API SDK v11 uses getFusedLocationProviderClient(this)
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { …
Run Code Online (Sandbox Code Playgroud)

android-intent android-pendingintent fusedlocationproviderapi location-updates fusedlocationproviderclient

5
推荐指数
0
解决办法
1265
查看次数

无法播放AVAudioPlayer中的文件中的文件

我在app的文档文件夹上有一个文件,我想播放它.

if NSFileManager.defaultManager().fileExistsAtPath(pathString) {
    let url = NSURL(fileURLWithPath:pathString, isDirectory: false)
    do {
        let player = try AVAudioPlayer(contentsOfURL:url)
        player.prepareToPlay()
        player.play()
    }
    catch let error as NSException {
        print(error)
    }
    catch {}
}
Run Code Online (Sandbox Code Playgroud)

文件系统上有一个文件,我检查了它的存在.此外,do块中的所有行都被执行.没有异常也没有错误,仍然没有播放声音.有哪些可能的问题,我该如何解决?

avaudioplayer nsfilemanager ios swift2

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