很高兴看到swift-playground的标签 - 喜欢看到这种情况继续下去.
我一直在修补Swift,我想知道MapKit是否可以在操场上使用,这样你就可以使用Quick Look功能,这样我就可以迭代并使用我的工作预览.我还没想出语法,所以我想问一下是否有人在游乐场环境中探讨过这个问题.我想我需要一些代码来建立它作为一个视图控制器.
import UIKit
import MapKit
// Sample UIView code which works great in the playground.
//var myView:UIView = UIView();
//myView.frame = CGRectMake(0, 0, 320, 560)
//myView.backgroundColor = UIColor.blueColor()
// Non working map code - no errors but can't figure out what to call or initiate to get the view in the Quick Look side.
var mapView = MKMapView();
mapView.region.center.latitude = mapView.userLocation.coordinate.latitude;
mapView.region.center.longitude = mapView.userLocation.coordinate.longitude;
mapView.region.span.latitudeDelta = 0.00725;
mapView.region.span.longitudeDelta = 0.00725;
Run Code Online (Sandbox Code Playgroud)
猜测我只是缺少一些简单的mapView来初始化自己等等.我喜欢解释的游乐场区域进行修补,只需要弄清楚它是否可以做mao功能而不是仅仅在Xcode中编写.swift文件和环境并获取更正式.
继续在Swift中玩游戏并尝试从C世界转换某些内容并且一直试图尝试各种语法.我有一些我想要初始化为结构数组的固定数据.这是我在C中如何做到这一点,但我无法在Swift中弄清楚,所以不要再猜测我会问那些了解更多的人.这是我的C代码.
#include <stdio.h>
typedef struct my_data {
const char *company;
const char *city;
const char *state;
float latitude;
float longitude;
} my_data;
int main() {
my_data data[2]={
{ "Joes Crab Shack", "Miami", "FL", 30.316599, -119.050254},
{ "Jims Crab Shack", "Los Angeles", "CA", 35.316599, -112.050254}
};
}
Run Code Online (Sandbox Code Playgroud)
在Swift中我可以创建一个类似的结构...
struct my_data {
var company = String();
var city = String();
var state = String();
var latitude:Float;
var longitude:Float;
}
Run Code Online (Sandbox Code Playgroud)
现在我陷入了如何声明和初始化固定数据,就像我在C中所做的那样.猜测它很简单并且语法正确让我感到困惑.我想将初始化样式保持为与C类似的格式,因为我可以轻松地从文件中提取和格式化这些数据.
我有一个工作循环来为一些工作数据点的标题和副标题元素设置注释.我想在同一个循环结构中做的是将引脚颜色设置为紫色而不是默认值.我无法弄清楚我需要做些什么来点击我的theMapView来相应地设置引脚.
我的工作循环和一些尝试......
....
for var index = 0; index < MySupplierData.count; ++index {
// Establish an Annotation
myAnnotation = MKPointAnnotation();
... establish the coordinate,title, subtitle properties - this all works
self.theMapView.addAnnotation(myAnnotation) // this works great.
// In thinking about PinView and how to set it up I have this...
myPinView = MKPinAnnotationView();
myPinView.animatesDrop = true;
myPinView.pinColor = MKPinAnnotationColor.Purple;
// Now how do I get this view to be used for this particular Annotation in theMapView that I am iterating through??? …Run Code Online (Sandbox Code Playgroud) 我一直在玩UISegmentedControl,并希望根据需要进行管理.我在故事板上创建了它,连接了插座和动作.它可视化很好,并且动作indexchanged上的回调有效.我想在可视化之前做一些其他的管理,所以我坚持如何正确访问故事板中的mysegmentedControl我设置.看起来像一个愚蠢的问题,但我找不到我可以与当前相关的UISegmentedControl的Swift示例.
class ViewController: UIViewController, MKMapViewDelegate {
...
//playing around with a Segmented Control
@IBOutlet var mysegmentedControl : UISegmentedControl
// Any time the button is clicked in the Segmented Control the index changes so we catch it to do something.
@IBAction func indexChanged(sender : UISegmentedControl) {
// This all works fine and it prints out the value of 3 on any click
println("# of Segments = \(sender.numberOfSegments)")
switch sender.selectedSegmentIndex {
case 0:
println("first segement clicked")
case 1:
println("second segment clicked")
case …Run Code Online (Sandbox Code Playgroud) 目标是根据存储在结构数组中的某些值自定义引脚颜色.
根据这里的一些帮助,我实现了以下viewForAnnotation委托方法,并且可以在基于结构数据数组大小的循环中迭代地调用此委托方法.因此,如果我想将所有引脚设置为一种颜色,例如紫色(这是下面代码中的注释行),它就可以工作.
问题是,当我根据数组中的值设置颜色时,它会通过此代码,但不会考虑任何案例值将其设置为备用颜色,并且所有内容都转到红色引脚(看似默认).我打印出状态并进行了调试,知道它已经进入开关并相应地设置了pinColor,但它们似乎并没有坚持下去.
func mapView(aMapView: MKMapView!,
viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
let theindex = mystructindex // grab the index from a global to be used below
if annotation is MKUserLocation {
//return nil so map view draws "blue dot" for standard user location
return nil
}
let reuseId = "pin"
var pinView = aMapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
//println("Pinview was nil")
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
// Preventive if …Run Code Online (Sandbox Code Playgroud) 我有一些旧的 Swift 代码,用于编译和工作,我使用 .append 动态构建数据结构。升级到几个较新的编译器版本后,我收到了可怕的“调用中的额外参数”错误。我将代码简化为:
struct EHSearch {
let EHcategory : String = ""
let EHname : String = ""
}
var myEHSearch = [EHSearch]()
// Call to dynamically append the results
// Extra argument: 'EHcategory' in call
myEHSearch.append(EHSearch(EHcategory: "Food", EHname: "Joes Crab Shack"))
Run Code Online (Sandbox Code Playgroud)
到目前为止,我在搜索导致此问题的原因发生变化时看不到任何内容,因此在此寻求指导。