Hi everyone,
I'm trying to follow the Landmarks project tutorial and got stuck in the second part, in the second section (Create the Row View), step 6 where it says "Modify the text view to use the landmark property’s name." This must be done in the LandmarkRow.swift file.
https://developer.apple.com/tutorials/swiftui/building-lists-and-navigation
The tutorial shows that after doing this, the preview will display the name of the landmark (Turtle Rock) instead of the usual "Hello world" greeting. But my replacement is not happening. Moreover, from this point on, the preview stops working. There are no error messages in the code, except for the message that the preview could not be performed.
I checked and rewrote the code several times, replaced the data source files, but nothing helped.
At the same time, the preview works well in other view files.
I can't figure out what's wrong with my code? Any ideas as to what the reason will be is appreciated.
Below is the code of two files, the LandmarkRow.swift, where view does not work, the second is ModelData.swift and it is related to the previous one.
LandmarkRow.swift
import SwiftUI
struct LandmarkRow: View {
var landmark: Landmark
var body: some View {
HStack {
landmark.image
.resizable()
.frame(width: 50, height: 50)
Text(landmark.name)
Spacer()
}
}
}
struct LandmarkRow_Previews: PreviewProvider {
static var previews: some View {
LandmarkRow(landmark: landmarks[0])
}
}
ModelData.swift
import Foundation
var landmarks: [Landmark] = load("landmarkData.json")
func loadT: Decodable(_ filename: String) - T {
let data: Data
guard let file = Bundle.main.url(forResource: filename, withExtension: nil)
else {
fatalError("Couldn't find \(filename) in main bundle")
}
do {
data = try Data(contentsOf: file)
} catch {
fatalError("Couldn't load \(filename) from main bundle:\n\(error)")
}
do {
let decoder = JSONDecoder()
return try decoder.decode(T.self, from: data)
} catch {
fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)")
}
}
Post
Replies
Boosts
Views
Activity
I create a Detail screen using a standard TableViewController. The TableView-Content option is Static Cells, the Table View Sell-Style option is Basic. The label is used by default, not custom. With the help of this line of code below, I try to pass a value to the label:
tableView.cellForRow(at: [0,0])?.textLabel?.text = item!.itemID
The value comes to the controller, I see it, but for some reason it is not displayed on the label and displays a nil in the debugger.
What could be the mistake?
Hi everybody,
There is one strange problem in my application. When the application is launched on the phone for the first time, it does not work for the first six seconds. After that everything works fine. The app also works fine when re-launched on the phone and when launched on the simulator.
The application itself is small and not complicated, all data is stored on the device. It uses a Tab Bar Controller with three tabs. Another strange feature is that if I click on the icons in these first six seconds, then nothing works right away, but then all the commands are executed. Those, it looks like the application has accepted the commands, but it takes a long time to execute them.
Has anyone faced such a problem? What could be causing this?
Any ideas are appreciated.
Swift 5.2, Xcode 11.6, macOS Catalina 10.5.6
Now I only see top posts. But it would be better to see all the new topics that have been created in recent days.
Hi everybody,
I am a beginner developer and in my Shopping List application I faced a problem of a massive controller. I have read several articles on this topic. They helped me to understand some things, but they don't solve this problem completely. Therefore, I decided myself to try to organize the structure of the view controller in terms of the tasks to be solved. For example, my application has a table view controller (screen) for list of items. This TVC should perform the following main tasks:
Show list of Items.
Select items for Shopping.
In turn, the first main task includes the following actions:
Show data in a table
Supporting Edit Actions - Add, Delete, and Change Data.
Supporting Usability Action - Sort (reorder, rearrange) Data.
The second main task includes the following actions:
Mark Items with a Checkmarks
In addition, this TVC should to save and load data.
Common / General Supporting Tasks for Both Main Tasks:
Save data to the archive file
Load Data from the archive file
When I implemented all the functions of the controller, I got the following real structure:
Supporting Common / General Task: Load data
Load data from database
Load/Update data after changing in other View Controllers
Main Task 1: Show data in a table.
numberOfSections
numberOfRowsInSection
cellForRowAt
Supporting Edit Task for Main Task 1: Add a new item.
Calling a method from an Extension
Supporting Edit Tasks for Main Task 1: Delete and Change actions
leadingSwipeActions
deleteAction
changeAction
Unwind Action - Return from Change Screen for Cancel button
Supporting Usability Task for Main Task 1: Sort list of items.
Show sort/reorder/rearrange button and its title
Which rows can be reordered (Allow to reorder)
Reorder items in list
Remove Delete button from screen
Main Task 2: Select items (mark them with checkmarks)
didSelectRowAt
Supporting Usability Task for Main Task 2: Clear all Checkmarks.
Uncheck all items button
Supporting Common / General Task: Persist Data (Save data to the archive file)
Save/persist data before View will disappear
viewWillDisappear
Could you tell me is this approach optimal? May I have some other problems later which I do not know about yet?
Any ideas are appreciated.
Hi everyone,can someone explain to me why, every time I reinstall the xcode, a message appears that I need to install additional components? Is this the way it should be, or is it some kind of hacker program trying to install on my computer?Any help would appreciate.
Today I have reinstall Xcode (11.3.1, Catalina).As soon as I launched it, such a message appeared:Install additional required componentsXcode requires additional components to support running and debugging. Choose Install to add required components.Can someone explain to me what these additional components are? And why aren't they included in Xcode by default?Maybe there is some of the employees of the Apple company on this forum who could explain this issue? Because I have a suspicion that some malware calls itself Xcode and wants to install something harmful on my computer.
I use Xcode every day. Today, a message suddenly appeared - "Developer Tools Access needs to take control of another process for debugging to continue"This has not happened before. I recently installed a clean Catalina, Xcode 11.3, swift 5.1Did anyone have such a message? Could you help me understand what to do is better in this situation?
When I use Xcode it regular shows me a message "Xcode requires additional components..."It was earlier, when I used Hi Sierra and Xcode 10, and today, when I installed Catalina and Xcode 11.What is it? Is it normally or it is a problem? Can anybody help me to make clear this question?
Hi everyone,I try to pass data from View Controller through Tab Bar Controller and Navigation Controller to Table View Controller. Screenshot of storyboard is bellow.https://drive.google.com/open?id=1GW6gaDxfK1zaEOk2aPE8izkS5B67ldZLMy code is next:1. Send data from View Controller@IBAction func addToCarButton(_ sender: UIButton) {
tabBarController?.selectedIndex = 1
let navVC = tabBarController?.viewControllers![1] as! UINavigationController//
let cartTableViewController = navVC.topViewController as! CartTableViewController
cartTableViewController.titleItem = titleLabel.text
cartTableViewController.image = SomeImage(photo: imageView.image!)
}2. Get data in Table View Controllerimport UIKit
class CartTableViewController: UITableViewController {
// MARK: - Store data
// Create variables for receiving data (title and data from image) from VC
var titleItem: String?
var image: SomeImage?
// Create shopping cart - array of selected items
var cartItems = [Cart]()
var cartItem: Cart?
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
//Load data from archive file
cartItems = Cart.loadFromFile()
// Create a new item
if titleItem != nil, image != nil {
print("titleItem in Cart View Appear - \(String(describing: titleItem))")
cartItem = Cart(title: titleItem!, image: image!)
}
// Add new item to shopping cart
if cartItem != nil {
cartItems.append(cartItem!)
}
tableView.reloadData()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cartItems.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SecondCell", for: indexPath)
// Pass title to cell
cell.textLabel?.text = cartItems[indexPath.row].title
// Retrieve image from array of archive file and convert from Data to UIImage format
let image = UIImage(data: cartItems[indexPath.row].image.photo)
// Pass image to cell:
cell.imageView?.image = image
return cell
}
// MARK: - Save data to archieve file
override func viewWillDisappear(_ animated: Bool) {
Cart.saveToFile(cartItems: cartItems)
tableView.reloadData()
}
// Clear cart
@IBAction func clearCartButton(_ sender: UIBarButtonItem) {
cartItems.removeAll()
tableView.reloadData()
}But data are appear only after second click. I.e., I add item 1 and cart is empty. When I add item 2, cart has item 1. If then I add item 3, card has item 1 and item 2, and go on.I have done link bellow on gif that shows how it works.https://drive.google.com/open?id=1_ilwKHXTRBT250zB4At7UsbWJ09fQARlI tried to use different options, but I can not understand what is happening here.Could you give me advice how to fix it?