NSInvalidArgumentException : 尝试从 objects[0] 中插入 nil

Posted

技术标签:

【中文标题】NSInvalidArgumentException : 尝试从 objects[0] 中插入 nil【英文标题】:NSInvalidArgumentException : attempt to insert nil from objects[0] 【发布时间】:2018-02-19 15:47:58 【问题描述】:

我有一个UIPageViewControllerDataSource,初始化如下:

[<Register ("FlatImagesPageViewDataSource")>]
type FlatImagesPageViewDataSource() as x = 
    inherit UIPageViewControllerDataSource()

let mutable isinitialised = false

let mutable flatImages : List<UIImage> = List.Empty

let mutable parentView : UIPageViewController = null

let mutable controllers : List<UIViewController> = List.Empty

let viewControllerAtIndex(index : int) = 
    let mutable result : UIViewController = null
    if flatImages.Length = 0 || index >= flatImages.Length then
        result <- null
    else 
        let controller = new FlatImagesContentViewController(new IntPtr())
        controller.GetImage <- flatImages.[index]
        controller.GetPageIndex <- index 
        if controllers.Length = 0 then
            ((controller :> UIViewController )::controllers) |> ignore
            int(0) |> ignore
        else 
            let listc  = List.toArray controllers
            listc.[0] <- (controller :> UIViewController)
            controllers <- Array.toList listc
            int(0) |> ignore
        result <- controller
    result


member this.GetParentView 
    with get() = parentView 
    and set(value) = (
        parentView <- value
        controllers <- Array.toList parentView.ViewControllers
    )

member this.GetFlatImages 
    with get() = flatImages 
    and set(value) = (
        flatImages <- value
        if not isinitialised then 
            parentView.View.UserInteractionEnabled <- true
            if flatImages.Length > 0 then 
                let initialcontroller = viewControllerAtIndex(0)
                let mutable viewControllers : UIViewController list = []
                viewControllers <- initialcontroller::viewControllers
                parentView.SetViewControllers(viewControllers |> List.toArray,UIPageViewControllerNavigationDirection.Forward,true,null)
                controllers <- viewControllers
            isinitialised <- true
        else
            ((controllers.[0]) :?> FlatImagesContentViewController).GetImage <- flatImages.[((parentView.ViewControllers.[0]) :?> FlatImagesContentViewController).GetPageIndex] 
    )

override x.GetNextViewController(pageViewController : UIPageViewController, contentController : UIViewController) = 
    let mutable returnController : UIViewController = null
    if flatImages.Length > 0 then 
        let curr_index = (contentController :?> FlatImagesContentViewController).GetPageIndex
        Console.WriteLine("Attempting after with index ")
        Console.WriteLine(curr_index.ToString())
        if curr_index < flatImages.Length - 1 then 
            returnController <- viewControllerAtIndex(curr_index + 1)
    returnController

override x.GetPreviousViewController(pageViewController : UIPageViewController, contentController : UIViewController) =
    let mutable returnController : UIViewController = null
    if flatImages.Length > 0 then 
        let curr_index = (contentController :?> FlatImagesContentViewController).GetPageIndex
        Console.WriteLine("Attempting before with index ")
        Console.WriteLine(curr_index.ToString())
        if curr_index > 0 then 
            returnController <- viewControllerAtIndex(curr_index - 1)
    returnController

override x.GetPresentationCount(pageViewController : UIPageViewController) =
    Conversions.nint(flatImages.Length)

override x.GetPresentationIndex(pageViewController : UIPageViewController) = 
    let mutable returnVal = 0
    if flatImages.Length > 0 then 
        returnVal <- (controllers.[0] :?> FlatImagesContentViewController).GetPageIndex
    Conversions.nint(returnVal)

这段代码崩溃就行了:

parentView.SetViewControllers(viewControllers |> List.toArray,UIPageViewControllerNavigationDirection.Forward,true,null)

在此代码块中:

member this.GetFlatImages 
with get() = flatImages 
and set(value) = (
    flatImages <- value
    if not isinitialised then 
        parentView.View.UserInteractionEnabled <- true
        if flatImages.Length > 0 then 
            let initialcontroller = viewControllerAtIndex(0)
            let mutable viewControllers : UIViewController list = []
            viewControllers <- initialcontroller::viewControllers
            parentView.SetViewControllers(viewControllers |> List.toArray,UIPageViewControllerNavigationDirection.Forward,true,null)
            controllers <- viewControllers
        isinitialised <- true
    else
        ((controllers.[0]) :?> FlatImagesContentViewController).GetImage <- flatImages.[((parentView.ViewControllers.[0]) :?> FlatImagesContentViewController).GetPageIndex] 
)

以下例外:

我不明白,因为我检查了initialController 绝对不是null,因此我不明白这个异常来自哪里。

【问题讨论】:

【参考方案1】:

我注意到当你想使用parentView.SetViewControllers(...) 时,viewControllers 只有一个对象。你如何构建你的UIPageViewController

如果你使用UIPageViewControllerSpineLocation.Mid或默认UIPageViewControllerSpineLocation(默认为Mid),在设置它的ViewControls时至少需要两个控件。

所以你可以尝试修改初始方法,如:

new UIPageViewController(UIPageViewControllerTransitionStyle.PageCurl, 
                         UIPageViewControllerNavigationOrientation.Horizontal,
                         UIPageViewControllerSpineLocation.Min)

【讨论】:

以上是关于NSInvalidArgumentException : 尝试从 objects[0] 中插入 nil的主要内容,如果未能解决你的问题,请参考以下文章

将 JSON 作为参数传递 swift