尝试推送视图控制器时应用崩溃

时间:2017-03-13 19:32:44

标签: swift xcode crash viewcontroller

所以我有以下代码块:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


    // Recall cell to call further cell data
    let cell = tableView.cellForRow(at: indexPath) as! FollowersCell

    var i: Int = 0;

    // if user tapped on himself, go home. If not, go guest 
    if cell.usernameLabel.text == PFUser.current()!.username! {
        let home = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
        self.navigationController?.pushViewController(home, animated: true)
    } else {
        print("You made it inside the else - clause")
        print(cell.usernameLabel.text!) // this prints out the correct username no matter which user I click on
        guestName.append(cell.usernameLabel.text!)
        print(guestName[i]) // this should print out the same as cell.usernameLabel.text (I think?) but only prints out whichever user I press the first time during runtime
        print(i) // this should print 0,1,2,3,... incrementing each time I press a given user, but it always prints only 0
        i = i + 1 // incrementing i by one
        let guest = self.storyboard?.instantiateViewController(withIdentifier: "GuestViewController") as? GuestViewController
        print("Made it this far")

        if guest == nil {
            print("The view controller is nil for some reason")
        } else {
            print("The view controller is not nil but it still doesn't work")
        }
        //self.navigationController?.pushViewController(guest!, animated: true)
    }
}

每当我点击应该调用此函数的视图时,都会在日志中打印以下内容:

You made it inside the else - clause
test3
test3
0
Made it this far
The view controller is not nil but it still doesn't work

正如您所看到的,我已经提供了一些检查点和行,以打印出有关我的程序状态的信息,并且我在代码中也添加了一些注释,请查看这些注释因为他们解释了我遇到麻烦的一些事情。然而,这是导致我最麻烦的最后一行(被评论出来的)。出于某种原因,每当我取消注释该行时,我的应用程序崩溃时出现以下错误:Error message,我在日志中收到以下消息:

libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

是否有人能够看到问题所在?从代码中可以看出,我试图推送的视图控制器不是零。我已经被困在这几天了,似乎无法弄明白。

EDIT 这里是GuestViewController:

import UIKit
import Parse

var guestName = [String]()

class GuestViewController: UICollectionViewController {

    // UI Objects 
    var refresher: UIRefreshControl!
    var page: Int = 10

    // Arrays to hold data from server
    var uuidArray = [String]()
    var pictureArray = [PFFile]()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.alwaysBounceVertical = true
        self.navigationItem.title = guestName.last?.uppercased()

        self.navigationItem.hidesBackButton = true
        let backButton = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(GuestViewController.back(_:)))
        self.navigationItem.leftBarButtonItem = backButton

        // Swipe to go back
        let backSwipe = UISwipeGestureRecognizer(target: self, action: #selector(GuestViewController.back(_:)))
        backSwipe.direction = UISwipeGestureRecognizerDirection.right
        self.view.addGestureRecognizer(backSwipe)

        // Pull to refresh
        refresher = UIRefreshControl()
        refresher.addTarget(self, action: #selector(GuestViewController.refresh), for: UIControlEvents.valueChanged)
        collectionView?.addSubview(refresher)

        // Load posts 
        loadPosts()

    }

    func back(_ sender : UIBarButtonItem) {
        _ = self.navigationController?.popViewController(animated: true)

        if !guestName.isEmpty {
            guestName.removeLast()
        }
    }

    func refresh() {
        collectionView?.reloadData()
        refresher.endRefreshing()
    }

    func loadPosts() {
        let query = PFQuery(className: "posts")
        query.whereKey("username", equalTo: guestName.last!)
        query.limit = page
        query.findObjectsInBackground { (objects, error) in
            if error != nil {
                print(error!.localizedDescription)
            } else {
                for object in objects! {
                    self.uuidArray.append(object.value(forKey: "uuid") as! String)
                    self.pictureArray.append(object.value(forKey: "picture") as! PFFile)
                }
                self.collectionView?.reloadData()
            }
        }
    }

    // Cell number
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureArray.count
    }

    // Cell configuration 
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! pictureCell
        pictureArray[indexPath.row].getDataInBackground { (data, error) in
            if error != nil {
                print(error!.localizedDescription)
            } else {
                cell.picture.image = UIImage(data: data!)
            }
        }
        return cell
    }

    // header configuration 
    override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "Header", for: indexPath) as! HeaderViewController


        // Load data of guest 

        let infoQuery = PFQuery(className: "_User")
        infoQuery.whereKey("username", equalTo: guestName.last!)
        infoQuery.findObjectsInBackground { (objects, error) in
            if error != nil {
                print(error!.localizedDescription)
            } else {

                // shown wrong user
                if objects!.isEmpty {
                    print("Wrong user")
                }

                // find user information 
                for object in objects! {
                    header.fullnameLabel.text = (object.object(forKey: "fullname") as? String)?.uppercased()
                    header.bioLabel.text  = object.object(forKey: "bio") as? String
                    header.bioLabel.sizeToFit()
                    let profilePictureFile: PFFile = (object.object(forKey: "profilePicture") as? PFFile)!
                    profilePictureFile.getDataInBackground(block: { (data, error) in
                        header.profilePicture.image = UIImage(data: data!)

                    })
                }
            }
        }

        // Show if current user is following guest

        let followQuery = PFQuery(className: "Follow")
        followQuery.whereKey("follower", equalTo: PFUser.current()!.username!)
        followQuery.whereKey("following", equalTo: guestName.last!)
        followQuery.countObjectsInBackground { (count, error) in
            if error != nil {
                print(error!.localizedDescription)
            } else {
                if count == 0 {
                    header.profileButton.setTitle("Follow", for: .normal)
                    header.bioLabel.backgroundColor = .lightGray
                } else {
                    header.profileButton.setTitle("Following", for: .normal)
                    header.profileButton.backgroundColor = .green

                }
            }
        }

        // Count statistics 

        // count posts 
        let posts = PFQuery(className: "posts")
        posts.whereKey("username", equalTo: guestName.last!)
        posts.countObjectsInBackground { (count, error) in
            if error != nil {
                print(error!.localizedDescription)
            } else {
                header.posts.text = "\(count)"
            }
        }

        // count followers 
        let followers = PFQuery(className: "posts")
        posts.whereKey("following", equalTo: guestName.last!)
        posts.countObjectsInBackground { (count, error) in
            if error != nil {
                print(error!.localizedDescription)
            } else {
                header.followers.text = "\(count)"
            }
        }

        // count followings 
        let followings = PFQuery(className: "posts")
        posts.whereKey("follower", equalTo: guestName.last!)
        followings.countObjectsInBackground { (count, error) in
            if error != nil {
                print(error!.localizedDescription)
            } else {
                header.following.text = "\(count)"
            }
        }

        // Implement tap gestures 

        // tap to post 
        let postsTap = UITapGestureRecognizer(target: self, action: #selector(GuestViewController.postsTap))
        postsTap.numberOfTapsRequired = 1
        header.posts.isUserInteractionEnabled = true
        header.posts.addGestureRecognizer(postsTap)

        // tap to followers
        let followersTap = UITapGestureRecognizer(target: self, action: #selector(GuestViewController.followersTap))
        followersTap.numberOfTapsRequired = 1
        header.followers.isUserInteractionEnabled = true
        header.followers.addGestureRecognizer(followersTap)

        // tap to followings 
        let followingsTap = UITapGestureRecognizer(target: self, action: #selector(GuestViewController.followingsTap))
        followingsTap.numberOfTapsRequired = 1
        header.following.isUserInteractionEnabled = true
        header.following.addGestureRecognizer(followingsTap)




        return header
    }

    func postsTap() {

        if !pictureArray.isEmpty {
            let index = NSIndexPath(item: 0, section: 0)
            self.collectionView?.scrollToItem(at: index as IndexPath, at: UICollectionViewScrollPosition.top, animated: true)
        }

    }

    func followersTap() {

        user = guestName.last!
        category = "followers"
        let followers = self.storyboard?.instantiateViewController(withIdentifier: "FollowersViewController") as! FollowersViewController
        self.navigationController?.pushViewController(followers, animated: true)
    }

    func followingsTap() {

        user = guestName.last!
        category = "followings"
        let followings = self.storyboard?.instantiateViewController(withIdentifier: "FollowersViewController") as! FollowersViewController
        self.navigationController?.pushViewController(followings, animated: true)
    }
}

这里是从开始到崩溃的完整日志:

2017-03-13 20:28:26.093035 w2c7[14482:1381876] [] nw_host_stats_add_src recv too small, received 24, expected 28
2017-03-13 20:28:26.100218 w2c7[14482:1381876] [] ____nwlog_simulate_crash_inner_block_invoke dlopen CrashReporterSupport failed
2017-03-13 20:28:26.100529 w2c7[14482:1381876] [] __nwlog_err_simulate_crash simulate crash failed "nw_socket_set_common_sockopts setsockopt SO_NOAPNFALLBK failed: [42] Protocol not available"
2017-03-13 20:28:26.101709 w2c7[14482:1381876] [] nw_socket_set_common_sockopts setsockopt SO_NOAPNFALLBK failed: [42] Protocol not available, dumping backtrace:
        [x86_64] libnetcore-856.30.16
    0   libsystem_network.dylib             0x000000010ea4a666 __nw_create_backtrace_string + 123
    1   libnetwork.dylib                    0x000000010f4e3006 nw_socket_add_input_handler + 3164
    2   libnetwork.dylib                    0x000000010f4c0555 nw_endpoint_flow_attach_protocols + 3768
    3   libnetwork.dylib                    0x000000010f4bf572 nw_endpoint_flow_setup_socket + 563
    4   libnetwork.dylib                    0x000000010f4be298 -[NWConcrete_nw_endpoint_flow startWithHandler:] + 2612
    5   libnetwork.dylib                    0x000000010f4d9ae1 nw_endpoint_handler_path_change + 1261
    6   libnetwork.dylib                    0x000000010f4d9510 nw_endpoint_handler_start + 570
    7   libnetwork.dylib                    0x000000010f4f11f9 nw_endpoint_resolver_start_next_child + 2240
    8   libdispatch.dylib                   0x000000010e7c7978 _dispatch_call_block_and_release + 12
    9   libdispatch.dylib                   0x000000010e7f10cd _dispatch_client_callout + 8
    10  libdispatch.dylib                   0x000000010e7cee17 _dispatch_queue_serial_drain + 236
    11  libdispatch.dylib                   0x000000010e7cfb4b _dispatch_queue_invoke + 1073
    12  libdispatch.dylib                   0x000000010e7d2385 _dispatch_root_queue_drain + 720
    13  libdispatch.dylib                   0x000000010e7d2059 _dispatch_worker_thread3 + 123
    14  libsystem_pthread.dylib             0x000000010eb9a4de _pthread_wqthread + 1129
    15  libsystem_pthread.dylib             0x000000010eb98341 start_wqthread + 13
2017-03-13 20:28:26.119252 w2c7[14482:1381876] [] nw_host_stats_add_src recv too small, received 24, expected 28
2017-03-13 20:28:26.124506 w2c7[14482:1381876] [] __nwlog_err_simulate_crash simulate crash already simulated "nw_socket_set_common_sockopts setsockopt SO_NOAPNFALLBK failed: [42] Protocol not available"
2017-03-13 20:28:26.125618 w2c7[14482:1381876] [] nw_socket_set_common_sockopts setsockopt SO_NOAPNFALLBK failed: [42] Protocol not available, dumping backtrace:
        [x86_64] libnetcore-856.30.16
    0   libsystem_network.dylib             0x000000010ea4a666 __nw_create_backtrace_string + 123
    1   libnetwork.dylib                    0x000000010f4e3006 nw_socket_add_input_handler + 3164
    2   libnetwork.dylib                    0x000000010f4c0555 nw_endpoint_flow_attach_protocols + 3768
    3   libnetwork.dylib                    0x000000010f4bf572 nw_endpoint_flow_setup_socket + 563
    4   libnetwork.dylib                    0x000000010f4be298 -[NWConcrete_nw_endpoint_flow startWithHandler:] + 2612
    5   libnetwork.dylib                    0x000000010f4d9ae1 nw_endpoint_handler_path_change + 1261
    6   libnetwork.dylib                    0x000000010f4d9510 nw_endpoint_handler_start + 570
    7   libnetwork.dylib                    0x000000010f4f11f9 nw_endpoint_resolver_start_next_child + 2240
    8   libdispatch.dylib                   0x000000010e7c7978 _dispatch_call_block_and_release + 12
    9   libdispatch.dylib                   0x000000010e7f10cd _dispatch_client_callout + 8
    10  libdispatch.dylib                   0x000000010e7cee17 _dispatch_queue_serial_drain + 236
    11  libdispatch.dylib                   0x000000010e7cfb4b _dispatch_queue_invoke + 1073
    12  libdispatch.dylib                   0x000000010e7d2385 _dispatch_root_queue_drain + 720
    13  libdispatch.dylib                   0x000000010e7d2059 _dispatch_worker_thread3 + 123
    14  libsystem_pthread.dylib             0x000000010eb9a4de _pthread_wqthread + 1129
    15  libsystem_pthread.dylib             0x000000010eb98341 start_wqthread + 13
2017-03-13 20:28:26.127269 w2c7[14482:1381876] [] nw_host_stats_add_src recv too small, received 24, expected 28
2017-03-13 20:28:26.131263 w2c7[14482:1381876] [] __nwlog_err_simulate_crash simulate crash already simulated "nw_socket_set_common_sockopts setsockopt SO_NOAPNFALLBK failed: [42] Protocol not available"
2017-03-13 20:28:26.131965 w2c7[14482:1381876] [] nw_socket_set_common_sockopts setsockopt SO_NOAPNFALLBK failed: [42] Protocol not available, dumping backtrace:
        [x86_64] libnetcore-856.30.16
    0   libsystem_network.dylib             0x000000010ea4a666 __nw_create_backtrace_string + 123
    1   libnetwork.dylib                    0x000000010f4e3006 nw_socket_add_input_handler + 3164
    2   libnetwork.dylib                    0x000000010f4c0555 nw_endpoint_flow_attach_protocols + 3768
    3   libnetwork.dylib                    0x000000010f4bf572 nw_endpoint_flow_setup_socket + 563
    4   libnetwork.dylib                    0x000000010f4be298 -[NWConcrete_nw_endpoint_flow startWithHandler:] + 2612
    5   libnetwork.dylib                    0x000000010f4d9ae1 nw_endpoint_handler_path_change + 1261
    6   libnetwork.dylib                    0x000000010f4d9510 nw_endpoint_handler_start + 570
    7   libnetwork.dylib                    0x000000010f4f11f9 nw_endpoint_resolver_start_next_child + 2240
    8   libdispatch.dylib                   0x000000010e7c7978 _dispatch_call_block_and_release + 12
    9   libdispatch.dylib                   0x000000010e7f10cd _dispatch_client_callout + 8
    10  libdispatch.dylib                   0x000000010e7cee17 _dispatch_queue_serial_drain + 236
    11  libdispatch.dylib                   0x000000010e7cfb4b _dispatch_queue_invoke + 1073
    12  libdispatch.dylib                   0x000000010e7d2385 _dispatch_root_queue_drain + 720
    13  libdispatch.dylib                   0x000000010e7d2059 _dispatch_worker_thread3 + 123
    14  libsystem_pthread.dylib             0x000000010eb9a4de _pthread_wqthread + 1129
    15  libsystem_pthread.dylib             0x000000010eb98341 start_wqthread + 13
2017-03-13 20:28:26.133267 w2c7[14482:1381876] [] nw_host_stats_add_src recv too small, received 24, expected 28
2017-03-13 20:28:26.136343 w2c7[14482:1381876] [] __nwlog_err_simulate_crash simulate crash already simulated "nw_socket_set_common_sockopts setsockopt SO_NOAPNFALLBK failed: [42] Protocol not available"
2017-03-13 20:28:26.136837 w2c7[14482:1381876] [] nw_socket_set_common_sockopts setsockopt SO_NOAPNFALLBK failed: [42] Protocol not available, dumping backtrace:
        [x86_64] libnetcore-856.30.16
    0   libsystem_network.dylib             0x000000010ea4a666 __nw_create_backtrace_string + 123
    1   libnetwork.dylib                    0x000000010f4e3006 nw_socket_add_input_handler + 3164
    2   libnetwork.dylib                    0x000000010f4c0555 nw_endpoint_flow_attach_protocols + 3768
    3   libnetwork.dylib                    0x000000010f4bf572 nw_endpoint_flow_setup_socket + 563
    4   libnetwork.dylib                    0x000000010f4be298 -[NWConcrete_nw_endpoint_flow startWithHandler:] + 2612
    5   libnetwork.dylib                    0x000000010f4d9ae1 nw_endpoint_handler_path_change + 1261
    6   libnetwork.dylib                    0x000000010f4d9510 nw_endpoint_handler_start + 570
    7   libnetwork.dylib                    0x000000010f4f11f9 nw_endpoint_resolver_start_next_child + 2240
    8   libdispatch.dylib                   0x000000010e7c7978 _dispatch_call_block_and_release + 12
    9   libdispatch.dylib                   0x000000010e7f10cd _dispatch_client_callout + 8
    10  libdispatch.dylib                   0x000000010e7cee17 _dispatch_queue_serial_drain + 236
    11  libdispatch.dylib                   0x000000010e7cfb4b _dispatch_queue_invoke + 1073
    12  libdispatch.dylib                   0x000000010e7d2385 _dispatch_root_queue_drain + 720
    13  libdispatch.dylib                   0x000000010e7d2059 _dispatch_worker_thread3 + 123
    14  libsystem_pthread.dylib             0x000000010eb9a4de _pthread_wqthread + 1129
    15  libsystem_pthread.dylib             0x000000010eb98341 start_wqthread + 13
Followings loaded
2017-03-13 20:28:30.369257 w2c7[14482:1381917] [] nw_host_stats_add_src recv too small, received 24, expected 28
2017-03-13 20:28:30.373519 w2c7[14482:1381917] [] __nwlog_err_simulate_crash simulate crash already simulated "nw_socket_set_common_sockopts setsockopt SO_NOAPNFALLBK failed: [42] Protocol not available"
2017-03-13 20:28:30.374350 w2c7[14482:1381917] [] nw_socket_set_common_sockopts setsockopt SO_NOAPNFALLBK failed: [42] Protocol not available, dumping backtrace:
        [x86_64] libnetcore-856.30.16
    0   libsystem_network.dylib             0x000000010ea4a666 __nw_create_backtrace_string + 123
    1   libnetwork.dylib                    0x000000010f4e3006 nw_socket_add_input_handler + 3164
    2   libnetwork.dylib                    0x000000010f4c0555 nw_endpoint_flow_attach_protocols + 3768
    3   libnetwork.dylib                    0x000000010f4bf572 nw_endpoint_flow_setup_socket + 563
    4   libnetwork.dylib                    0x000000010f4be298 -[NWConcrete_nw_endpoint_flow startWithHandler:] + 2612
    5   libnetwork.dylib                    0x000000010f4d9ae1 nw_endpoint_handler_path_change + 1261
    6   libnetwork.dylib                    0x000000010f4d9510 nw_endpoint_handler_start + 570
    7   libnetwork.dylib                    0x000000010f4f11f9 nw_endpoint_resolver_start_next_child + 2240
    8   libdispatch.dylib                   0x000000010e7c7978 _dispatch_call_block_and_release + 12
    9   libdispatch.dylib                   0x000000010e7f10cd _dispatch_client_callout + 8
    10  libdispatch.dylib                   0x000000010e7cee17 _dispatch_queue_serial_drain + 236
    11  libdispatch.dylib                   0x000000010e7cfb4b _dispatch_queue_invoke + 1073
    12  libdispatch.dylib                   0x000000010e7d2385 _dispatch_root_queue_drain + 720
    13  libdispatch.dylib                   0x000000010e7d2059 _dispatch_worker_thread3 + 123
    14  libsystem_pthread.dylib             0x000000010eb9a4de _pthread_wqthread + 1129
    15  libsystem_pthread.dylib             0x000000010eb98341 start_wqthread + 13
2017-03-13 20:28:30.993304 w2c7[14482:1381876] [] nw_host_stats_add_src recv too small, received 24, expected 28
2017-03-13 20:28:30.996557 w2c7[14482:1381876] [] __nwlog_err_simulate_crash simulate crash already simulated "nw_socket_set_common_sockopts setsockopt SO_NOAPNFALLBK failed: [42] Protocol not available"
2017-03-13 20:28:30.997102 w2c7[14482:1381876] [] nw_socket_set_common_sockopts setsockopt SO_NOAPNFALLBK failed: [42] Protocol not available, dumping backtrace:
        [x86_64] libnetcore-856.30.16
    0   libsystem_network.dylib             0x000000010ea4a666 __nw_create_backtrace_string + 123
    1   libnetwork.dylib                    0x000000010f4e3006 nw_socket_add_input_handler + 3164
    2   libnetwork.dylib                    0x000000010f4c0555 nw_endpoint_flow_attach_protocols + 3768
    3   libnetwork.dylib                    0x000000010f4bf572 nw_endpoint_flow_setup_socket + 563
    4   libnetwork.dylib                    0x000000010f4be298 -[NWConcrete_nw_endpoint_flow startWithHandler:] + 2612
    5   libnetwork.dylib                    0x000000010f4d9ae1 nw_endpoint_handler_path_change + 1261
    6   libnetwork.dylib                    0x000000010f4d9510 nw_endpoint_handler_start + 570
    7   libnetwork.dylib                    0x000000010f4f11f9 nw_endpoint_resolver_start_next_child + 2240
    8   libdispatch.dylib                   0x000000010e7c7978 _dispatch_call_block_and_release + 12
    9   libdispatch.dylib                   0x000000010e7f10cd _dispatch_client_callout + 8
    10  libdispatch.dylib                   0x000000010e7cee17 _dispatch_queue_serial_drain + 236
    11  libdispatch.dylib                   0x000000010e7cfb4b _dispatch_queue_invoke + 1073
    12  libdispatch.dylib                   0x000000010e7d2385 _dispatch_root_queue_drain + 720
    13  libdispatch.dylib                   0x000000010e7d2059 _dispatch_worker_thread3 + 123
    14  libsystem_pthread.dylib             0x000000010eb9a4de _pthread_wqthread + 1129
    15  libsystem_pthread.dylib             0x000000010eb98341 start_wqthread + 13
You made it inside the else - clause
test3
test3
0
Made it this far
The view controller is not nil but it still doesn't work

4 个答案:

答案 0 :(得分:0)

在包含GuestViewController的实际故事板本身上,是故事板ID设置吗?在拼写/大小写方面,它是否与您在代码中设置的完全相同?此外,对于这行代码,而不是“as”之后的问号,将其置于“!”。

let guest = self.storyboard?.instantiateViewController(withIdentifier: "GuestViewController") as? GuestViewController.

您是否确定故事板上的GuestViewController,该类未设置?或者可能设置错误的班级?最后,您确定所有内容都嵌入在NavigationController中吗?

答案 1 :(得分:0)

经过近一年的时间,你肯定必须解决它,但为了以防万一,检查一下,如果你从故事板中的另一个VC复制并粘贴了组件,那么这些组件的出口是"拔掉的& #34;或在故事板中删除,用于该VC

答案 2 :(得分:-1)

在调用guest后推送视图控制器时,请在没有感叹号的情况下尝试。或者在您设置GuestViewController值的行中guest之后添加一个惊叹号。这样做会将其声明为Optional(GuestViewController)。这意味着您的变量可以是GuestViewController,也可以是nil。

如果您未将其声明为Optional,则未定义引用guest!。此外,如果您尚未将其声明为Optional,则无需检查它是否为nil。

答案 3 :(得分:-2)

你确定你的ViewController是NavigationController的孩子吗? 你尝试过测试了吗?

{{1}}