gpt4 book ai didi

swift - 尝试推送 View Controller 时应用程序崩溃

转载 作者:搜寻专家 更新时间:2023-11-01 05:44:49 26 4
gpt4 key购买 nike

所以我有以下代码块:

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)
}
}

每当我点击应该调用此函数的 View 时,以下内容就会打印到日志中:

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)

有人能看出问题所在吗?从代码中可以看出,我尝试推送的 View Controller 不是零。我已经坚持了好几天了,似乎无法弄清楚。

编辑这是 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

最佳答案

已经过去了将近一年,你肯定已经解决了它,但为了以防万一,检查一下,如果你从 Storyboard中的另一个 VC 复制和粘贴组件,这些组件的 socket 是否“拔掉”或移除在 Storyboard 中,对于那个 VC

关于swift - 尝试推送 View Controller 时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42771960/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com