Swift 3 - inout String不能转换为String

时间:2016-10-26 20:18:52

标签: ios swift

为什么它在Swift 3中不起作用?如何转换它?

    static void
    create_base_gui(appdata_s *ad)
    {

// Create the window
ad->win = elm_win_util_standard_add(PACKAGE, PACKAGE);
elm_win_conformant_set(ad->win, EINA_TRUE);

// Advertise which rotations are supported by the application; the
// device_orientation callback is used to do the actual rotation when
// the system detects the device's orientation has changed
if (elm_win_wm_rotation_supported_get(ad->win)) {
    int rots[4] = { 0, 90, 180, 270 };
    elm_win_wm_rotation_available_rotations_set(ad->win, (const int *)(&rots), 4);
}

// Add a callback on the "delete,request" event; it is emitted when
// the system closes the window
evas_object_smart_callback_add(ad->win, "delete,request", win_delete_request_cb, NULL);

// Alternatively, elm_win_autodel_set() can be used to close
// the window (not the application) automatically
// with the Back button, for example
// elm_win_autodel_set(ad->win, EINA_TRUE);

// Create the conformant
ad->conform = elm_conformant_add(ad->win);

// Set the conformant use as much horizontal and vertical space as
// possible, that is, expand in both directions
evas_object_size_hint_weight_set(ad->conform, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);

// Set the conformant as the resize object for the window:
// the window and the conformant grow together
// in proportion to each other
elm_win_resize_object_add(ad->win, ad->conform);


// Create the naviframe
ad->naviframe = elm_naviframe_add(ad->conform);
elm_object_content_set(ad->conform, ad->naviframe);

// Show the box
evas_object_show(ad->conform);

// Show the conformant since all widgets are hidden by default
evas_object_show(ad->conform);

// Create the box
Evas_Object *box = elm_box_add(ad->naviframe);

// Set the box vertical
elm_box_horizontal_set(box, EINA_FALSE);

// The box expands when its contents need more space
evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);

// The box fills the available space
evas_object_size_hint_align_set(box, EVAS_HINT_FILL, EVAS_HINT_FILL);

// Add the box in the naviframe container
elm_naviframe_item_push(ad->naviframe, "Hello World", NULL, NULL, box, NULL);

// Show the box
evas_object_show(box);

Evas_Object *box_one = elm_box_add(box);
elm_box_horizontal_set(box_one, EINA_TRUE);
evas_object_size_hint_weight_set(box_one, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);


// Create the label
Evas_Object *label = elm_label_add(box_one);
// The label expands when its contents need more space
evas_object_size_hint_weight_set(label, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
// The box fills the available space on the horizontal axis and is
// centered on the vertical axis (placed at 0.5 vertically, that is, in the
// middle)
evas_object_size_hint_align_set(label, 0.5, EVAS_HINT_FILL);

// Set the text for the label and set formatting through the HTML tags:
// - "Hello World!" centered on the first line
// - skip a line
// - Add a longer text that does not fit on a single line but wraps at
//   the word boundaries
elm_object_text_set(label,
        "<align=center>Hello World!</align><br>"
        "<br>"
        "<wrap = word>Clicking on the button below closes the application.</wrap>");

// Add the label at the end of the box
elm_box_pack_end(box_one, label);

// Show the label
evas_object_show(label);

// Create the button
Evas_Object *button = elm_button_add(box_one);

// The box expands when its contents need more space
evas_object_size_hint_weight_set(button, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);

// The button fills the available space on the horizontal axis and is
// placed at the bottom of the vertical axis (1 is the end of the axis,
// the coordinates start at (0, 0) on the top-left corner
evas_object_size_hint_align_set(button, 0.5, EVAS_HINT_FILL);

// Set the text for the button
elm_object_text_set(button, "Close!");

// Add a callback on the button for the "clicked" event; implementation of
// the callback is below
//evas_object_smart_callback_add(button, "clicked", clicked_cb, NULL);

// Add the widget at the end of the box; since the axis starts in the top left
// corner and the box is vertical, the end of the box is below the label
//elm_box_pack_end(box_one, button);

// Show the button
evas_object_show(button);

/* Show window after base gui is set up */
evas_object_show(ad->win);

}

当我改变...到...&lt;它工作正常,但我认为结果可能是相同的

var valorTemp: String = "44,52"
valorTemp = valorTemp.substring(with: Range<String.Index>(valorTemp.startIndex...valorTemp.characters.index(valorTemp.endIndex, offsetBy: -2)))

谢谢!

1 个答案:

答案 0 :(得分:2)

在Swift 3中,两个范围运算符.....<返回两种不同的类型。适用于String.IndexClosedRange<String.Index>Range<String.Index>

substring(with:)仅为Range<String.Index>定义,您无法使用初始化语法将ClosedRange<String.Index>转换为Range<String.Index>

您可以尝试明确使用..<(只需要修改下限):

valorTemp = valorTemp.substring(with: valorTemp.startIndex..<valorTemp.characters.index(valorTemp.endIndex, offsetBy: -1))

或者,String的下标有一些重载,包括Range<String.Index>ClosedRange<String.Index>

valorTemp = valorTemp[valorTemp.startIndex...valorTemp.characters.index(valorTemp.endIndex, offsetBy: -2)]

在您使用substring(to:)的情况下,这将是一个不错的选择(需要使用&#34;修改后的&#34;索引):

valorTemp = valorTemp.substring(to: valorTemp.characters.index(valorTemp.endIndex, offsetBy: -1))