tvOS - 如何从 topshelf ServiceProvider.swift 中的 class 检索对象?
tvOS - how to retrieve objects from class in topshelf ServiceProvider.swift?
在我的 tvOS 应用程序中,我有一个 class 可以生成要使用的示例数据。
import Foundation
import UIKit
class SampleData {
class func initializeSampleData() -> [Object] {
var data = [Object]()
// Cities
let obj_01 = Object(title: "Dubai",
country: "United Arab Emirates",
cDescription: "Dubai has positioned itself as a global metropolitan city with its majestic buildings.",
image: #imageLiteral(resourceName: "city_dubai"),
category: "Cities")
let obj_02 = Object(title: "Paris",
country: "France",
cDescription: "The Eiffel Tower is one of the world's more recognizable building landscapes.",
image: #imageLiteral(resourceName: "city_paris"),
category: "Cities")
let obj_03 = Object(title: "Tokoyo",
country: "Japan",
cDescription: "One of the most crowded cities in the world. So many advertisements!",
image: #imageLiteral(resourceName: "city_tokoyo"),
category: "Cities")
let obj_04 = Object(title: "Madrid",
country: "Spain",
cDescription: "One of Europe's oldest cities, Madrid skyscape is beautiful year round.",
image: #imageLiteral(resourceName: "city_madrid"),
category: "Cities")
let obj_05 = Object(title: "New York",
country: "United States",
cDescription: "A view of the Empire State building and its surrounding buildings.",
image: #imageLiteral(resourceName: "city_newyork"),
category: "Cities")
// Sports
let obj_06 = Object(title: "Olypics logo",
country: "Brazil",
cDescription: "Gotta love watching the Olympics whenever, whereever!",
image: #imageLiteral(resourceName: "sports_olympics"),
category: "Sports")
let obj_07 = Object(title: "Rio 2016 Olympics",
country: "Brazil",
cDescription: "Kicking off the start of the 2016 Olympic games. So much festivities!",
image: #imageLiteral(resourceName: "sports_rio2016"),
category: "Sports")
let obj_08 = Object(title: "Rio 2016 Olympics Mascot",
country: "Brazil",
cDescription: "A wonderful aerial view of Rio De Janeiro behind the crazy mascot on the suspended lift!",
image: #imageLiteral(resourceName: "sports_mascot"),
category: "Sports")
let obj_09 = Object(title: "Cycling Atheletes",
country: "Brazil",
cDescription: "Athletes from around the world come and compete against each other in the Olympic games.",
image: #imageLiteral(resourceName: "sports_cycling"),
category: "Sports")
let obj_10 = Object(title: "Runner Athletes",
country: "Brazil",
cDescription: "Athletes from around the world come and compete against each other in the Olympic games.",
image: #imageLiteral(resourceName: "sports_runners"),
category: "Sports")
// Foods
let obj_11 = Object(title: "Sweets",
country: "Italy",
cDescription: "Who, tell me who does not love Italian sweets! Drewl.",
image: #imageLiteral(resourceName: "foods_sweets"),
category: "Foods")
let obj_12 = Object(title: "Octopus",
country: "Italy",
cDescription: "A delicasy, and a slimey one at that. It is thought to be a natural aphrodesiac.",
image: #imageLiteral(resourceName: "foods_octopus"),
category: "Foods")
let obj_13 = Object(title: "Bolognese Pasta",
country: "Italy",
cDescription: "Pasta in all its forms originate from Italian kitchens.",
image: #imageLiteral(resourceName: "foods_bolognese"),
category: "Foods")
let obj_14 = Object(title: "Bruschetta",
country: "Italy",
cDescription: "Bruschetta is an antipasto consisting of grilled bread rubbed with garlic and topped with olive oil and salt.",
image: #imageLiteral(resourceName: "foods_bruschetta"),
category: "Foods")
let obj_15 = Object(title: "Chocolate Budino",
country: "Italy",
cDescription: "Warm chocolate budino is a luscious cross between cake and pudding. Yum!",
image: #imageLiteral(resourceName: "foods_chocolate_budino"),
category: "Foods")
data.append(obj_01)
data.append(obj_02)
data.append(obj_03)
data.append(obj_04)
data.append(obj_05)
data.append(obj_06)
data.append(obj_07)
data.append(obj_08)
data.append(obj_09)
data.append(obj_10)
data.append(obj_11)
data.append(obj_12)
data.append(obj_13)
data.append(obj_14)
data.append(obj_15)
return data
}
}
我希望能够调用相同的 class 函数来从我的顶层 ServiceProvider.swift 中检索相同的数据。当我尝试以下代码时,Xcode 给我一个 "use of unresolved identifier SampleData" :
var topShelfItems: [TVContentItem] {
let objects = SampleData.initializeSampleData()
//return []
}
非常感谢任何帮助。
当你想在目标(iphone/ipad/tv/watch/other服务)之间共享文件时,点击Xcode中的文件,你会在屏幕的右侧看到一个"target membership" 选项。 Select 无论您希望您的代码可访问哪个目标。
既然我已经回答了我自己的问题,我的建议是:不要像我那样将数据检索到您的顶层。有一种预定义的方法可以将数据放入 topshelf。只需从此处下载 Apple 的代码示例并查看代码:https://developer.apple.com/library/content/samplecode/UICatalogFortvOS/Introduction/Intro.html
在我的 tvOS 应用程序中,我有一个 class 可以生成要使用的示例数据。
import Foundation
import UIKit
class SampleData {
class func initializeSampleData() -> [Object] {
var data = [Object]()
// Cities
let obj_01 = Object(title: "Dubai",
country: "United Arab Emirates",
cDescription: "Dubai has positioned itself as a global metropolitan city with its majestic buildings.",
image: #imageLiteral(resourceName: "city_dubai"),
category: "Cities")
let obj_02 = Object(title: "Paris",
country: "France",
cDescription: "The Eiffel Tower is one of the world's more recognizable building landscapes.",
image: #imageLiteral(resourceName: "city_paris"),
category: "Cities")
let obj_03 = Object(title: "Tokoyo",
country: "Japan",
cDescription: "One of the most crowded cities in the world. So many advertisements!",
image: #imageLiteral(resourceName: "city_tokoyo"),
category: "Cities")
let obj_04 = Object(title: "Madrid",
country: "Spain",
cDescription: "One of Europe's oldest cities, Madrid skyscape is beautiful year round.",
image: #imageLiteral(resourceName: "city_madrid"),
category: "Cities")
let obj_05 = Object(title: "New York",
country: "United States",
cDescription: "A view of the Empire State building and its surrounding buildings.",
image: #imageLiteral(resourceName: "city_newyork"),
category: "Cities")
// Sports
let obj_06 = Object(title: "Olypics logo",
country: "Brazil",
cDescription: "Gotta love watching the Olympics whenever, whereever!",
image: #imageLiteral(resourceName: "sports_olympics"),
category: "Sports")
let obj_07 = Object(title: "Rio 2016 Olympics",
country: "Brazil",
cDescription: "Kicking off the start of the 2016 Olympic games. So much festivities!",
image: #imageLiteral(resourceName: "sports_rio2016"),
category: "Sports")
let obj_08 = Object(title: "Rio 2016 Olympics Mascot",
country: "Brazil",
cDescription: "A wonderful aerial view of Rio De Janeiro behind the crazy mascot on the suspended lift!",
image: #imageLiteral(resourceName: "sports_mascot"),
category: "Sports")
let obj_09 = Object(title: "Cycling Atheletes",
country: "Brazil",
cDescription: "Athletes from around the world come and compete against each other in the Olympic games.",
image: #imageLiteral(resourceName: "sports_cycling"),
category: "Sports")
let obj_10 = Object(title: "Runner Athletes",
country: "Brazil",
cDescription: "Athletes from around the world come and compete against each other in the Olympic games.",
image: #imageLiteral(resourceName: "sports_runners"),
category: "Sports")
// Foods
let obj_11 = Object(title: "Sweets",
country: "Italy",
cDescription: "Who, tell me who does not love Italian sweets! Drewl.",
image: #imageLiteral(resourceName: "foods_sweets"),
category: "Foods")
let obj_12 = Object(title: "Octopus",
country: "Italy",
cDescription: "A delicasy, and a slimey one at that. It is thought to be a natural aphrodesiac.",
image: #imageLiteral(resourceName: "foods_octopus"),
category: "Foods")
let obj_13 = Object(title: "Bolognese Pasta",
country: "Italy",
cDescription: "Pasta in all its forms originate from Italian kitchens.",
image: #imageLiteral(resourceName: "foods_bolognese"),
category: "Foods")
let obj_14 = Object(title: "Bruschetta",
country: "Italy",
cDescription: "Bruschetta is an antipasto consisting of grilled bread rubbed with garlic and topped with olive oil and salt.",
image: #imageLiteral(resourceName: "foods_bruschetta"),
category: "Foods")
let obj_15 = Object(title: "Chocolate Budino",
country: "Italy",
cDescription: "Warm chocolate budino is a luscious cross between cake and pudding. Yum!",
image: #imageLiteral(resourceName: "foods_chocolate_budino"),
category: "Foods")
data.append(obj_01)
data.append(obj_02)
data.append(obj_03)
data.append(obj_04)
data.append(obj_05)
data.append(obj_06)
data.append(obj_07)
data.append(obj_08)
data.append(obj_09)
data.append(obj_10)
data.append(obj_11)
data.append(obj_12)
data.append(obj_13)
data.append(obj_14)
data.append(obj_15)
return data
}
}
我希望能够调用相同的 class 函数来从我的顶层 ServiceProvider.swift 中检索相同的数据。当我尝试以下代码时,Xcode 给我一个 "use of unresolved identifier SampleData" :
var topShelfItems: [TVContentItem] {
let objects = SampleData.initializeSampleData()
//return []
}
非常感谢任何帮助。
当你想在目标(iphone/ipad/tv/watch/other服务)之间共享文件时,点击Xcode中的文件,你会在屏幕的右侧看到一个"target membership" 选项。 Select 无论您希望您的代码可访问哪个目标。
既然我已经回答了我自己的问题,我的建议是:不要像我那样将数据检索到您的顶层。有一种预定义的方法可以将数据放入 topshelf。只需从此处下载 Apple 的代码示例并查看代码:https://developer.apple.com/library/content/samplecode/UICatalogFortvOS/Introduction/Intro.html