编辑:有代码复制/粘贴选项 我在“cellForRowAtIndexPath”又名“TableView”函数中遇到错误,该函数说“动物”实例不能用于“动物”类型。
这是AppDelegate类
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
return true
}
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let animal = Animal.animals[indexPath.row]
let cell = tableView.dequeueReusableCellWithIdentifier("Animal name", forIndexPath: indexPath) as UITableViewCell
/*var name: String
var shortDescription: String
var longDescription: String
// Configure the cell...
*/
cell.textLabel?.text = AnimalListTableViewController.name
cell.detailTextLabel?.text = AnimalListTableViewController.shortDescription
return cell
}
}
这是AnimalListTableViewController类
import UIKit
class AnimalListTableViewController: UITableViewController
{ var name: String
var shortDescription: String
var longDescription: String
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 12}
override func prepareForSegue(segue: UIStoryboardSegue,
sender: AnyObject?){
if let detailViewController = segue.destinationViewController as? DetailViewController, let indexPath = self.tableView.indexPathForSelectedRow {
detailViewController.animal = animals[indexPath.row]}}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")}
let animals = [
Animal(name: "Cow",
shortDescription: "Cattle",
longDescription: "A cow is a mature female and bull of an adult male of a bovine family. A heifer is a female cow that hasn't had a calf yet. Cattle is the name for the whole cow family. THere are about 920 different breeds of cows in the world."),
Animal(name: "Bird",
shortDescription: "Usually small, has wings, feathers, and can fly.",
longDescription: "A warm-blooded egg-laying vertebrate distinguished by the possession of feathers, wings, and a beak and (typically) by being able to fly."),
Animal(name: "Dolphin",
shortDescription: "A large fish",
longDescription: "A small gregarious toothed whale that typically has a beaklike snout and a curved fin on the back. Dolphins have become well known for their sociable nature and high intelligence."),
Animal(name: "Dog",
shortDescription: "Man's best friend",
longDescription: "A domesticated carnivorous mammal that typically has a long snout, an acute sense of smell, and a barking, howling, or whining voice. It is widely kept as a pet or for work or field sports."),
Animal(name: "Zebra",
shortDescription: "A horse with white and black stripes",
longDescription: "An African wild horse with black-and-white stripes and an erect mane."),
Animal(name: "Owl",
shortDescription: "A large bird that usually comes out at night",
longDescription: "A nocturnal bird of prey with large forward-facing eyes surrounded by facial disks, a hooked beak, and typically a loud call."),
Animal(name: "Camel",
shortDescription: "A horse-like animal that travels the deserts",
longDescription: "A large, long-necked ungulate mammal of arid country, with long slender legs, broad cushioned feet, and either one or two humps on the back. Camels can survive for long periods without food or drink, chiefly by using up the fat reserves in their humps."),
Animal(name: "Lizard",
shortDescription: "A green animal that is very small and has four legs",
longDescription: "A reptile that typically has a long body and tail, four legs, movable eyelids, and a rough, scaly, or spiny skin."),
Animal(name: "Monkey",
shortDescription: "A furry animal that climbs trees.",
longDescription: "A small to medium-sized primate that typically has a long tail, most kinds of which live in trees in tropical countries."),
Animal(name: "Jaguar",
shortDescription: "A very fast animal with four legs and lots of fur",
longDescription: "A large, heavily built cat that has a yellowish-brown coat with black spots, found mainly in the dense forests of Central and South America."),
Animal(name: "Chicken",
shortDescription: "A small legged animal with a red nose.",
longDescription: "A domestic fowl kept for its eggs or meat, especially a young one."),
Animal(name: "Mouse",
shortDescription: "A very small creature with a tail.",
longDescription: "a small rodent that typically has a pointed snout, relatively large ears and eyes, and a long tail.")]}
这是Animal类
import UIKit
class Animal
{
var name: String
var shortDescription: String
var longDescription: String
init(name: String, shortDescription: String, longDescription: String)
{
self.name = name
self.shortDescription = shortDescription
self.longDescription = longDescription
}
let animals = [
Animal(name: "Cow",
shortDescription: "Cattle",
longDescription: "A cow is a mature female and bull of an adult male of a bovine family. A heifer is a female cow that hasn't had a calf yet. Cattle is the name for the whole cow family. THere are about 920 different breeds of cows in the world."),
Animal(name: "Bird",
shortDescription: "Usually small, has wings, feathers, and can fly.",
longDescription: "A warm-blooded egg-laying vertebrate distinguished by the possession of feathers, wings, and a beak and (typically) by being able to fly."),
Animal(name: "Dolphin",
shortDescription: "A large fish",
longDescription: "A small gregarious toothed whale that typically has a beaklike snout and a curved fin on the back. Dolphins have become well known for their sociable nature and high intelligence."),
Animal(name: "Dog",
shortDescription: "Man's best friend",
longDescription: "A domesticated carnivorous mammal that typically has a long snout, an acute sense of smell, and a barking, howling, or whining voice. It is widely kept as a pet or for work or field sports."),
Animal(name: "Zebra",
shortDescription: "A horse with white and black stripes",
longDescription: "An African wild horse with black-and-white stripes and an erect mane."),
Animal(name: "Owl",
shortDescription: "A large bird that usually comes out at night",
longDescription: "A nocturnal bird of prey with large forward-facing eyes surrounded by facial disks, a hooked beak, and typically a loud call."),
Animal(name: "Camel",
shortDescription: "A horse-like animal that travels the deserts",
longDescription: "A large, long-necked ungulate mammal of arid country, with long slender legs, broad cushioned feet, and either one or two humps on the back. Camels can survive for long periods without food or drink, chiefly by using up the fat reserves in their humps."),
Animal(name: "Lizard",
shortDescription: "A green animal that is very small and has four legs",
longDescription: "A reptile that typically has a long body and tail, four legs, movable eyelids, and a rough, scaly, or spiny skin."),
Animal(name: "Monkey",
shortDescription: "A furry animal that climbs trees.",
longDescription: "A small to medium-sized primate that typically has a long tail, most kinds of which live in trees in tropical countries."),
Animal(name: "Jaguar",
shortDescription: "A very fast animal with four legs and lots of fur",
longDescription: "A large, heavily built cat that has a yellowish-brown coat with black spots, found mainly in the dense forests of Central and South America."),
Animal(name: "Chicken",
shortDescription: "A small legged animal with a red nose.",
longDescription: "A domestic fowl kept for its eggs or meat, especially a young one."),
Animal(name: "Mouse",
shortDescription: "A very small creature with a tail.",
longDescription: "a small rodent that typically has a pointed snout, relatively large ears and eyes, and a long tail.")]
}
答案 0 :(得分:0)
你弄乱了类方法/属性和实例方法/属性。
如果动物属于阶级财产:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NHibernate;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using ControleUsuarios.Map;
using NHibernate.Tool.hbm2ddl;
namespace ControleUsuarios.BD {
public class BDConnect {
private static ISessionFactory session;
private static const String HOST = "localhost";
private static const String USER = "root";
private static const String PASSWORD = "";
private static const String DB = "usuario_db";
/** create a connection with database */
private static ISessionFactory createConnection() {
if (session != null)
return session;
//database configs
FluentConfiguration _config = Fluently.Configure().Database(MySQLConfiguration.Standard.ConnectionString(
x => x.Server(HOST).
Username(USER).
Password(PASSWORD).
Database(DB)
))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<UsuarioMap>())
.ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(false, true));
session = _config.BuildSessionFactory();
return session;
}
/** open a session to make transactions */
public static ISession openSession() {
return createConnection().OpenSession();
}
}
}
如果只是属性:
let animals = AnimalListTableViewController.animals
动物也是如此:
let animals = AnimalListTableViewController().animals
我建议你阅读swift book,以便清楚地了解代码。