Skip to content

Commit

Permalink
Moved CString logic outside unsafe block
Browse files Browse the repository at this point in the history
  • Loading branch information
simlay committed Aug 21, 2020
1 parent 2b9a3ab commit 9fbbe84
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions 2020-08-17-use-uikit-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,18 @@ fn add_label(label_text: String, root_view: UIView) {
ffi::CString,
convert::TryInto,
};

let text = CString::new(label_text.as_str()).expect("CString::new failed");
let text_ptr = text.as_ptr();
let text_length = label_text.len().try_into().unwrap();
unsafe {
let label = UILabel::alloc();
label.init();

let text = NSString(
NSString::alloc().initWithBytes_length_encoding_(
CString::new(label_text.as_str())
.expect("CString::new failed")
.as_ptr() as *mut std::ffi::c_void,
label_text.len().try_into().unwrap(),
text_ptr as *mut std::ffi::c_void,
text_length,
NSUTF8StringEncoding,
),
);
Expand Down

0 comments on commit 9fbbe84

Please sign in to comment.